aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Watch "Thread New topic
Author

Thread's

nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
class Q
{
int n;
synchronized int get()
{
System.out.println("got:"+n);
return n;
}
synchronized void put(int n)
{
this.n=n;
System.out.println("put:"+n);
}
}
class producer implements Runnable
{
Q q;
producer(Q q)
{
this.q=q;
new Thread(this,"producer").start();
}
public void run()
{
int i=0;
while(true)
{
q.put(i++);
}
}
}
class consumer implements Runnable
{
Q q;
consumer(Q q)
{
this.q=q;
new Thread(this,"consumer").start();
}
public void run()
{
while(true)
{
q.get();
}
}
}
class pc
{
public static void main(String[]args)
{
Q q=new Q();
new producer(q);
new consumer(q);
System.out.println("press control c to stop ");
}
}
hello,
In the above given programme i have a Q class inside which i have two synchronized method's got() and put().I have created two thread's inside the pc class and invoking those synchronized method's.
Now my question is when one thread is inside the synchronized method then no other thread can access any synchronized of the class on the same instance.But in the above programme both the thread's are able to execute the synchronized method's at the same time.Please throw some light on it,correct me if i am wrong.

QUESTION FROM COMPLETE REFERENCE PAGE NO 298.
Rajesh R
Greenhorn

Joined: Mar 02, 2001
Posts: 12
Hi Nithin,
I don't think that both threads are executing synchronized methods concurrently. The object is locked during the execution of synchronized method only. Let us assume that producer thread started first and started its run() method. it aquired the lock on q in first iteration of while loop. Then this thread executes the method of q, which is put(). This is the method which is synchronized. Therefore it aquires a lock on the object. When this put method completes and return to "producer.run()" method, it releases the lock on q. Between this iteration and next iteration of "producer.run()" method, the other thread can start execution and aquire a lock on q, meanwhile original producer has to wait.
If this is not something that you want, you need to use a diffrent logic to implement a producer-consumer relationship using wait and notify() method.
I hope I could answer your question.
Rajesh
Rajesh R
Greenhorn

Joined: Mar 02, 2001
Posts: 12
I am sorry, i want to eliminat one line from my previous email, which is "it aquired the lock on q in first iteration of while loop". Please read my previous email as follows:
Sorry for the confusion.
thanks,
Rajesh
Hi Nithin,
I don't think that both threads are executing synchronized methods concurrently. The object is locked during the execution of synchronized method only. Let us assume that producer thread started first and started its run() method. Then this thread executes the method of q, which is put(). This is the method which is synchronized. Therefore it aquires a lock on the object. When this put method completes and return to "producer.run()" method, it releases the lock on q. Between this iteration and next iteration of "producer.run()" method, the other thread can start execution and aquire a lock on q, meanwhile original producer has to wait.
If this is not something that you want, you need to use a diffrent logic to implement a producer-consumer relationship using wait and notify() method.
I hope I could answer your question.
Rajesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread's
 
Similar Threads
Thread's
Thread's
Thread's
thread's
static synchronized method