• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

synchronized - join

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test
{
public static void main(String args[])
{
try{
MyThread m=new MyThread();
MyThread m1=new MyThread();
m.start();
System.out.println("main method");
m.join(800);
m1.start();
System.out.println(" elapsed 800 milliseconds");
m1.join(1000);
System.out.println("elapsed 1000 milliseconds");
}catch(Exception ie){}
}//main
}//Test

class MyThread extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
try{ Thread.sleep(500);}catch(InterruptedException ie){}
System.out.println(getName() );
}//for
}//run
}//MyThread

Output:
main method
Thread-0
elapsed 800 milliseconds
Thread-0
Thread-1
Thread-0
Thread-1
elapsed 1000 milliseconds
Thread-0
Thread-1
Thread-0
Thread-1
Thread-1

I understood the output ..this way,

m.join(800)- main thread has waited for 800 milliseconds, then s.o.p statment is executed

m1.join(1000) - main thread has waited for 1000milliseconds,then s.o.p is executed, now main thread is in dead state. m, m1 threads are running.

When i add synchronized keyword to run method of MyThread class...

public synchronized void run(){ /*same code */}

output is
main method
Thread-0 //line-1
Thread-0
Thread-0
Thread-0
Thread-0//line5
elapsed 800 milliseconds
Thread-1//line7
Thread-1
Thread-1
Thread-1
Thread-1//line11
elapsed 1000 milliseconds

Here, run method is synchronized , so second thread (m1) had to wait till first thread completes execution... but
why main thread is waiting more than 800 milliseconds ( at m.join(800))
and why main thread is waiting more than 1000 milliseconds?
( at m.join(1000) ).

I expected the output of first S.o.p between Line1-Line5 and output of second s.o.p between Line7- Line11. Becuase API says,


public final void join(long millis)

Waits at most millis milliseconds for this thread to die. , but here main thread is waiting more than specified no. of milliseconds.

Can any one of you explain pls.
Thanx in advance

Naresh

[ December 18, 2005: Message edited by: Naresh Kumar ]atmost millis milliseconds
[ December 18, 2005: Message edited by: Naresh Kumar ]
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, it is very simple logic. I have just copied part of code for illustration. I assume run method is synchrnized.

1 MyThread m=new MyThread();
2 MyThread m1=new MyThread();
3 m.start();
4 System.out.println("main method");
5 m.join(800);
6 m1.start();
7 System.out.println(" elapsed 800 milliseconds");
8 m1.join(1000);
9 System.out.println("elapsed 1000 milliseconds");

at line 3, thread m is started and will become ready to run. soon after some time, its run method starts running as expected. so main will proceed at line 4 and prints message. at line 5, join is encountered and at the point, main is blocked for 800 ms. as per your expectation, it comes out exactly after 800 ms. and main continues at line 6. At that point m1 becomes "ready-to-run". now as run method is synchronized, it has to wait for m to complete and hence for free lock. so thread m's run method is completely executed before m1's run() method is entered. rest of the output is obvious.

Just keep in mind that,
join() call blocks main method.
join(long time) call blocks main method for 'time' ms.
when method is synchrnized, only one thread can enter and execute it completely before another thread is entered.
 
Naresh Gunda
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dilip,
Thanx 4 ur response. But you hv not cleared my doubt. Please refer the output in both the cases, and my doubt is,

why main thread is waiting more than 800 milliseconds ( at m.join(800))
and why main thread is waiting more than 1000 milliseconds? (output with synchronized run) ( at m.join(1000) ).???

Regards
Naresh
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naresh, perhaps this is because of the thread scheduler? You're creating three threads of execution (main, 0 and 1) and it's quite possible that main is waiting for only 800 ms but the thread scheduler is not letting main run. Instead, thread 0 is allowed to run to completion.
One very important point about thread scheduling: very little is guaranteed. So even though main doesn't want to wait to more than 800 ms, it's up to the scheduler if it'll let main run or thread 0 to run. Try changing the priorities of the threads and see what happens. Also run your code 5 times and see how the output behaves all 5 times.
HTH,
Sashi
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi naresh,
actually when u start the thread in which the run method is synchronized
first the thread acquires the lock on that object and executes the run method. during this process parallely the main thread get executed but due to the stmt obj.join()here the join method is synchronized, that is the main thread has to acquire the lock on the object on which it is invoking the join method but currently the lock is held by the first thread which u started earlier in the main thread so only after the thread completes the run method it relinquishes the lock then the main thread acquires the lock and executes the join method.

for all this the time taken to execute the join method is 500*no of iterations(due to first thread)hope u r clear with the concept
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with NareshAnkuskani reply.
 
vipul patel
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naresh,

once the sleep time is elapsed, it only makes thread 'ready to run' which means runnable. then it is totally up to the shedulling algorithm whether to pick that thread or not for running.

Also other checks for synchronization goes even if that thread is selected to run.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Naresh is absoulutely rite.
 
I child proofed my house but they still get in. Distract them with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic