This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
    Bookmark Topic Watch Topic
  • New Topic

Thread's

 
nitin sharma
Ranch Hand
Posts: 290
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
    Bookmark Topic Watch Topic
  • New Topic