为什么锁定主线程实例的对象有效?
Why does placing a lock on an object of the main thread's instance work?
我有几个关于多线程和等待的问题 Java 下面的代码。
/*
* Taken from
* https://www.wiley.com/en-us/Java+Programming%3A+24+Hour+Trainer%2C+2nd+Edition-p-9781118951453
*/
public class TestThreads3LambdaWait {
public static void main(String args[]) {
// Lambda expression for Market News
Runnable mn = () -> {
try {
for (int i = 0; i < 10; i++) {
Thread.sleep (1000); // sleep for 1 second
System.out.println( "The market is improving " + i);
}
} catch(InterruptedException e ) {
System.out.println(Thread.currentThread().getName()
+ e.toString());
}
};
Thread marketNews = new Thread(mn, "Market News");
marketNews.start();
// Lambda expression for Portfolio
Runnable port = () -> {
try {
for (int i = 0; i < 10; i++) {
Thread.sleep (700); // Sleep for 700 milliseconds
System.out.println( "You have " + (500 + i) +
" shares of IBM");
}
} catch(InterruptedException e ) {
System.out.println(Thread.currentThread().getName()
+ e.toString());
}
};
Thread portfolio = new Thread(port,"Portfolio data");
portfolio.start();
TestThreads3LambdaWait thisInstance = new TestThreads3LambdaWait();
synchronized (thisInstance) {
try {
thisInstance.wait(15000);
System.out.println("finished wait");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println( "The main method of TestThreads3Lambda is finished");
}
}
mn
和 port
执行完后会调用 notify()
吗?
- 收到通知的班长是谁? (由于
main()
是静态的,main()
中的代码未绑定到特定对象。)
- 为什么锁定主线程实例的对象(即,将通知发送到
thisInstance
,它与 main()
无关,因为 main()
是静态的).是否因为 mn
和 port
在静态方法中并因此绑定到每个实例,所以所有 TestThreads3LambdaWait
对象都被通知?
- 没有
- 无关紧要。此代码中没有任何通知 - 没有人在其中的任何地方调用
notify(All)
。
- 因为
Thread.sleep(15000)
也会这样做,这实际上是您的 wait
调用所做的,因为没有人通知。
我有几个关于多线程和等待的问题 Java 下面的代码。
/*
* Taken from
* https://www.wiley.com/en-us/Java+Programming%3A+24+Hour+Trainer%2C+2nd+Edition-p-9781118951453
*/
public class TestThreads3LambdaWait {
public static void main(String args[]) {
// Lambda expression for Market News
Runnable mn = () -> {
try {
for (int i = 0; i < 10; i++) {
Thread.sleep (1000); // sleep for 1 second
System.out.println( "The market is improving " + i);
}
} catch(InterruptedException e ) {
System.out.println(Thread.currentThread().getName()
+ e.toString());
}
};
Thread marketNews = new Thread(mn, "Market News");
marketNews.start();
// Lambda expression for Portfolio
Runnable port = () -> {
try {
for (int i = 0; i < 10; i++) {
Thread.sleep (700); // Sleep for 700 milliseconds
System.out.println( "You have " + (500 + i) +
" shares of IBM");
}
} catch(InterruptedException e ) {
System.out.println(Thread.currentThread().getName()
+ e.toString());
}
};
Thread portfolio = new Thread(port,"Portfolio data");
portfolio.start();
TestThreads3LambdaWait thisInstance = new TestThreads3LambdaWait();
synchronized (thisInstance) {
try {
thisInstance.wait(15000);
System.out.println("finished wait");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println( "The main method of TestThreads3Lambda is finished");
}
}
mn
和port
执行完后会调用notify()
吗?- 收到通知的班长是谁? (由于
main()
是静态的,main()
中的代码未绑定到特定对象。) - 为什么锁定主线程实例的对象(即,将通知发送到
thisInstance
,它与main()
无关,因为main()
是静态的).是否因为mn
和port
在静态方法中并因此绑定到每个实例,所以所有TestThreads3LambdaWait
对象都被通知?
- 没有
- 无关紧要。此代码中没有任何通知 - 没有人在其中的任何地方调用
notify(All)
。 - 因为
Thread.sleep(15000)
也会这样做,这实际上是您的wait
调用所做的,因为没有人通知。