java编程思想中关于线程同步的一段裁剪的代码,
对线程同步有点疑问:
class TwoCounter2 extends Thread {
public void run() {
while (true) {
synchronized(this) {
t1.setText(Integer.toString(count1++));
t2.setText(Integer.toString(count2++));
}
try {
sleep(500); //问题
} catch (InterruptedException e){}
}
}
}
问题:
1、当线程执行sleep(500)时,其他线程是否可以访问TwoCounter2对象的同步方法。
2、如果其它线程能访问TwoCounter2对象的同步方法,TwoCounter2它是以什么方式通知
等待线程的。(notify吗?)小弟在线,先谢了
对线程同步有点疑问:
class TwoCounter2 extends Thread {
public void run() {
while (true) {
synchronized(this) {
t1.setText(Integer.toString(count1++));
t2.setText(Integer.toString(count2++));
}
try {
sleep(500); //问题
} catch (InterruptedException e){}
}
}
}
问题:
1、当线程执行sleep(500)时,其他线程是否可以访问TwoCounter2对象的同步方法。
2、如果其它线程能访问TwoCounter2对象的同步方法,TwoCounter2它是以什么方式通知
等待线程的。(notify吗?)小弟在线,先谢了
据说最好用notifyAll