Originally posted by Valentin Crettaz:
Angela first, you should put the line which prints "Inside Run" inside the synchronized block too (or make the run method synchronized). Then you cannot say that the output is the same for the second and the third case because it may not be so on another platform. AS IS the code could well output
Inside Run
Inside Run
Inside Synchronized
Inside Synchronized
since nothing prevent two threads to enter the run method one after another (one of them will just be blocked when attempting to enter the syunchronized block).
In cases 1 and 2, the Thread are sharing the same run method but not in case 3 since they have different targets, one is an instance t of MyThread and the other is another new instance of MyThread, so the run method is not shared...
HIH
MyThread1 t = new MyThread1();
new Thread(t).start(); //1
new Thread(t).start(); //2
Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
3) Blocks synchronized on this object-reference, and synchronized methods share da same instance lock .
( like saying
public void run()
{
synchronized(this)
{
//do losta work
}
}
and
synchronized public void run()
{
//do losta work
}
is one n da same thing( as long as there is no statement outside da sync block ! )
)
1
Only Thread[Thread-0,5,main] 1
In synchronized
Only Thread[Thread-1,5,main] In synchronized
1
Only Thread[Thread-0,5,main] In synchronized
1
Only Thread[Thread-1,5,main] In synchronized
1. What if there is some code outside the synchronized() code block ?
Now suppose i want to get the sequence strictly as OUTPUT 2:
i do something as below , that is share the object between
the threads :
i change the main method as
MyThread1 t = new MyThread1();
new Thread().start(); //1
new Thread().start(); //2
Pls. correct if wrong
Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
...Does it depend upon the threading model of the system on which we run the program....
...Another clarification i wanted was w.r.t to code as you explained...
IS there something like default priority of a thread ?
And What is the priority of the main thread?
When i create/spawn threads from the main thread and print their priority it print
5 which is the NORM_PRIORITY .
Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
Originally posted by Gagan Indus:
Angela
3) Blocks synchronized on this object-reference, and synchronized methods share da same instance lock .
( like saying
<font color="red">
public void run()
{
synchronized(this)
{
//do losta work
}
}
</font>
and
<font color="red">
synchronized public void run()
{
//do losta work
}
</font>
is one n da same thing( as long as there is no statement outside da sync block ! )
)
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |