Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Having doubts about an example from topic Threads in KB.

 
Ranch Hand
Posts: 99
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys!Please clear my doubt in following example of threads in KB.

1. class ThreadA {
2. public static void main(String [] args) {
3. ThreadB b = new ThreadB();
4. b.start();
5.
6. synchronized(b) {
7. try {
8. System.out.println("Waiting for b to complete...");
9. b.wait();
10. } catch (InterruptedException e) {}
11. System.out.println("Total is: " + b.total);
12. }
13. }
14. }
15.
16. class ThreadB extends Thread {
17. int total;
18.
19. public void run() {
20. synchronized(this) {
21. for(int i=0;i<100;i++) {
22. total += i;
23. }
24. notify();
25. }
26. }
27. }

Now when line 4 is executed there are 2 threads-main thread & thread running for loop.Thread running for loop acquire lock on object b therefore main thread cannot enter synchronized block in line 6 until other thread is completed releasing lock on object b which main method can use to call wait method.What exactly happens is main thread execute b.wait() simultaneously or it wait for other thread to complete before entering synchronized block.

Thanks
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Anuj,

I see the syncronized keyword like an atomic operation, only one thread got the synchronization on the same object, not is possible that two threads run the synchronization simultaneously. Once the main thread or the threadB got the lock for the same object.

In this example if threadb got the lock for b object before the main thread, main thread wait on line 6. After Thread b finish his synchronized block, the lock is released and the main thread wait forever to a notify().

try to run this code:


The main thread call wait() after a notify(), so main thread wait forever in his wait() method. An spurios wake up can success too, but i dont know what exactly happen with the main thread in that situation, i think that the main thread is resumed and b.total variable is printed but i not sure, see the Object API
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just another point of view....
understand that, when you call the start() on the new thread, nobody says that the new thread is going into execution right now. THAT is up to the JVM thread scheduler. All the start really does is move the new thread in to the runnable state.

In the code that you wrote there are two possibilities:
a) when the main thread calls b.start(), the new thread will be moved to runnable state and the thread dispatcher will dispatch it immediately and take the lock (which is the path that you seem to be assuming in the body of your question), and

b) when the main thread calls b.start(), the new thread will be moved to runnable and the thread dispatcher will return to the main thread. In that case the main thread will hit the lock first.

The important thing to grasp is: calling start does NOT force execution of the new thread, it simply changes the new thread to runnable so that, at some time, the thread dispatcher CAN put it into execution. But it is up to the dispatcher when it will do so. Your code must be tolerant of either possibility.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider This Example:-

public class ThreadA {
public static void main(String [] args) {
ThreadB b = new ThreadB();
b.start();

synchronized(b) {
try {
System.out.println("Waiting for b to complete...");
b.wait();
System.out.println("after wait");
} catch (InterruptedException e) {}

System.out.println("Total is: " + b.total);
}
}
}

class ThreadB extends Thread {
int total;

public void run() {
System.out.println("thread B in run");
try{
Thread.currentThread().sleep(100);
}catch(InterruptedException e){

}

synchronized(this) {
for(int i=0;i<100;i++) {
total += i;
}
notify();
}
}
}

As per me when you call wait() it`s also release the lock from that sync object and go to sleep until other Thread not notify that Thread.

Still i will explore some about it, and try to clear it.
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic