L
lielar
Hi
Still stuck on threading question. (Someone will probably recognise
this)
I have the following thread class
<snip>-------------------------------------------------------
class MBThread extends Thread {
String name;
OrderedThread orderT;
MBThread(String name, OrderedThread orderT) {
this.name = name;
this.orderT = orderT;
}
public void run() {
orderT.display(name);
}
}
public class MBTHread {
public void display(String msg) {
synchronized(msg) {
for (int i=0; i<20; i++) {
System.out.println("Name= "+msg);
}
}
}
public static void main(String [] args) {
OrderedThread orderedT = new OrderedThread();
MBThread first = new MBThread("One", orderedT);
MBThread second = new MBThread("Two", orderedT);
first.start();
second.start();
}
}
<snip>-------------------------------------------------------
My Questions
--------------------
1) Here the string object is synchronised. What is the difference
between the string reference being synchronised as opposed to the
whole method? Does it mean that any thread can access the method but
only one thread can access the object?
2) If I synch another object would the outcome be different if it is
mutable (unlike String)?
3) What is the difference between synching the instance, the class
(MBThread.class), an object, and method?
Thanks
Still stuck on threading question. (Someone will probably recognise
this)
I have the following thread class
<snip>-------------------------------------------------------
class MBThread extends Thread {
String name;
OrderedThread orderT;
MBThread(String name, OrderedThread orderT) {
this.name = name;
this.orderT = orderT;
}
public void run() {
orderT.display(name);
}
}
public class MBTHread {
public void display(String msg) {
synchronized(msg) {
for (int i=0; i<20; i++) {
System.out.println("Name= "+msg);
}
}
}
public static void main(String [] args) {
OrderedThread orderedT = new OrderedThread();
MBThread first = new MBThread("One", orderedT);
MBThread second = new MBThread("Two", orderedT);
first.start();
second.start();
}
}
<snip>-------------------------------------------------------
My Questions
--------------------
1) Here the string object is synchronised. What is the difference
between the string reference being synchronised as opposed to the
whole method? Does it mean that any thread can access the method but
only one thread can access the object?
2) If I synch another object would the outcome be different if it is
mutable (unlike String)?
3) What is the difference between synching the instance, the class
(MBThread.class), an object, and method?
Thanks