• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

On threads

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test {
public static void main(String[] args) {
final Foo f = new Foo();
Thread t = new Thread(new Runnable() {
public void run() {
f.doStuff();
}
}, "Thread1");

Thread g = new Thread("Thread2") {
public void run() {
f.doStuff();
}
};
t.start();
g.start();
}
}

class Foo {
int x = 5;

public void doStuff() {
synchronized (this) {
if (x < 10) {
// nothing to do
try {
x += 5;
System.out.println(Thread.currentThread().getName()
+ " is waiting.");
this.wait();
System.out.println(Thread.currentThread().getName()
+ " is wait is over.");
} catch (InterruptedException ex) {
}
} else {
System.out.println("x is :" + x);
if (x >= 10) {
this.notify();
}
}
}
}
}




how can g.start() access the synchronized and call notify() code when the lock of the object is possessed by wait() which was executed earlier ?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Ripechancewoods" (previously "Siddalinga K M" and "siddu.sjce"),

You've been warned twice before about your display name. If you check the JavaRanch Naming Policy, you will see that we need folks to use real (or at least real-looking) names, with a first and a last name. (Initials are fine for the first name, but not the last.)
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The API
makes for interesting reading :


The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.


(my emphasis)

Hope this answers your question.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Siddu" -

Please change your display name immediately to comply to the JavaRanch naming policy.

Note that we take the naming policy seriously. If you do not change your name according to the policy, your account will be locked. You have been warned multiple times.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic