public void run(){
try{
for(int i = 15;i > 0;i--){
System.out.println(name + ": " + i);
Thread.sleep(200);
synchronized(this){ // 这里的synchronized(this)是指对象调用run()方法,还是wait()方法?
while(suspendFlag){
wait();
}
}
}
}public void run()
{
synchronized(target){
target.call(msg); // 这里的synchronized(target)
// 是target对象调用call()方法,对吧
}
}public class Thread1 implements Runnable { public void run() {
synchronized (this) { // 这里的synchronized (this)是ta、tb分别调用run()方法?
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " synchronized loop " + i);
}
}
} public static void main(String[] args) {
Thread1 t1 = new Thread1();
Thread ta = new Thread(t1, "A");
Thread tb = new Thread(t1, "B");
ta.start();
tb.start();
}
}
try{
for(int i = 15;i > 0;i--){
System.out.println(name + ": " + i);
Thread.sleep(200);
synchronized(this){ // 这里的synchronized(this)是指对象调用run()方法,还是wait()方法?
while(suspendFlag){
wait();
}
}
}
}public void run()
{
synchronized(target){
target.call(msg); // 这里的synchronized(target)
// 是target对象调用call()方法,对吧
}
}public class Thread1 implements Runnable { public void run() {
synchronized (this) { // 这里的synchronized (this)是ta、tb分别调用run()方法?
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " synchronized loop " + i);
}
}
} public static void main(String[] args) {
Thread1 t1 = new Thread1();
Thread ta = new Thread(t1, "A");
Thread tb = new Thread(t1, "B");
ta.start();
tb.start();
}
}
class Apple{
private int a;
public Apple(int num){
this.a = num;//this 是指当前对象,即使多线程也是一样道理的
}
}
while(suspendFlag){
wait();
}
这里重写了synchronized方法,你下面写的wait();
当然是调用wait();方法了,不过要当前对象拿到钥匙。
public void run()
{
synchronized(target){
target.call(msg); // 这里的synchronized(target)
// 是target对象调用call()方法,对吧
}
}
这个是对的
public void run() {
synchronized (this) { // 这里的synchronized (this)是ta、tb分别调用run()方法?
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " synchronized loop " + i);
}
}
}ta.start();
tb.start();
这里不一定是谁调用run方法,在本类中new的线程,你调用其start方法,他都会调用run方法
然后执行这个
synchronized (this) { // 这里的synchronized (this)是ta、tb分别调用run()方法?
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " synchronized loop " + i);
}