File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Simple Thread Doubt -from Jane's Notes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Simple Thread Doubt -from Jane Watch "Simple Thread Doubt -from Jane New topic

Simple Thread Doubt -from Jane's Notes

swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
In the Janes' Study Notes,there is a simple program which creates 2 threads and prints characters '!' and '*'.As per notes output shd be intermingled because the threads are running concurrently and are interleaved.
But when i run the same code, I do not get intermingled output.IS it perfectly the NOrmal behavior of Threads..?


Swati Kale
arie zwijnsma

Joined: Jul 28, 2001
Posts: 5
when i run the code the output is intermingled.
i suspect on your machine thread t1 is finished before t2 has started. you can try to set the priority of t1 lower than the priority of t2, if this doesnt help make the for loop more timeconsuming by changing the for loop to for instance
for (int i=0;i<100;i++){
for(int j=0;j<10000;j++){for(int k=0;k<10000;k++){}}
// bye
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
IMO, Such codes yield different o/p on different machines depending upon the speed of the system and OS.
Hope I am correct.
comments... welcome.
tvs sundaram
Mini Pilla
Ranch Hand

Joined: Jul 15, 2001
Posts: 112
I have another interesting output to share.
WHen i declaring i<10 it is ! followed by *
but when I am declaring i<100 or i<20 some
larger values it is all INTERMINGLED.So I
think it depends on the OS.

[This message has been edited by Rajani Katta (edited August 05, 2001).]
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1183
Try the following -
public class ThreadDemo extends Thread{
char c;
ThreadDemo(char c){
this.c = c;
public static void main(String args[]){
ThreadDemo t1 = new ThreadDemo('!');
ThreadDemo t2 = new ThreadDemo('*');
public void run(){

for (int i=0;i<100;i++)
try {
} catch (InterruptedException e) {}

William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
Thanx all.
When I use sleep(2) as suggested by Dan, the output is intermingled.
I guess,it is OS specific.
I agree. Here's the link:
subject: Simple Thread Doubt -from Jane's Notes
It's not a secret anymore!