java - synchronized 关键字给出了预期的输出,但对不同线程调用方法的顺序不满意
问题描述
下面是我实现 Runnable 接口的 3 个类(线程):
public class Thread1 implements Runnable {
Shared s;
public Thread1(Shared s){
this.s = s;
}
@Override
public void run() {
System.out.println("Sum of 10 and 20 by " + Thread.currentThread().getName() + " is "+ s.add(10, 20));
}
}
public class Thread2 implements Runnable {
Shared s;
public Thread2(Shared s){
this.s = s;
}
@Override
public void run() {
System.out.println("Sum of 100 and 200 by " + Thread.currentThread().getName() + " is " + s.add(100, 200));
}
}
public class Thread3 implements Runnable {
Shared s;
public Thread3(Shared s){
this.s = s;
}
@Override
public void run() {
System.out.println("Sum of 1000 and 2000 by " + Thread.currentThread().getName() + " is " + s.add(1000, 2000));
}
}
下面是这些线程之间共享其对象的类:
public class Shared {
private int x;
private int y;
synchronized public int add(int a, int b) {
x = a;
y = b;
try {
System.out.println(Thread.currentThread().getName() + "is going into sleep state");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return x + y;
}
}
最后这就是我启动线程并传递共享对象的方式:
public static void main(String[] args) {
Shared s = new Shared();
Thread t1 = new Thread(new Thread1(s), "Thread-1");
Thread t2 = new Thread(new Thread2(s), "Thread-2");
Thread t3 = new Thread(new Thread3(s), "Thread-3");
t1.start();
t2.start();
t3.start();
}
这是我得到的正确输出:
Thread-2is going into sleep state
Thread-1is going into sleep state
Sum of 100 and 200 by Thread-2 is 300
Thread-3is going into sleep state
Sum of 10 and 20 by Thread-1 is 30
Sum of 1000 and 2000 by Thread-3 is 3000
但是如果你在这里看到,Thread-1 在 Thread-2 完成它的工作之前就开始执行 add 方法(它是同步的)。由于 Thread-2 进入了睡眠状态,所以它自己也获得了锁,不应允许其他线程进入 add(...) 方法。Thread-1 或 Thread-3 只能在 Thread-2 完成后开始执行 add(...) 方法。所以,我期待的输出是:
Thread-2is going into sleep state
Sum of 100 and 200 by Thread-2 is 300
Thread-1is going into sleep state
Sum of 10 and 20 by Thread-1 is 30
Thread-3is going into sleep state
Sum of 1000 and 2000 by Thread-3 is 3000
请告诉我做错了什么,或者这就是输出的方式,如果是,请告诉原因。
解决方案
原因是System.out.println()
速度很慢。
您可以使用它从 run() 方法和add()
. 无法保证 sysout 中的消息一致。
我重写Shared
如下:
public class Shared {
private int x;
private int y;
synchronized public int add(int a, int b) {
x = a;
y = b;
try {
long now = System.currentTimeMillis() - start;
Thread.sleep(1000);
// notice FIRST sleep and THEN sysout
System.out.println(Thread.currentThread().getName() + " calculation has taken place at time " + now);
} catch (InterruptedException e) {
e.printStackTrace();
}
return x + y;
}
public static long start;
public static void main(String[] args) {
start = System.currentTimeMillis();
Shared s = new Shared();
Thread t1 = new Thread(new Thread1(s), "Thread-1");
Thread t2 = new Thread(new Thread2(s), "Thread-2");
Thread t3 = new Thread(new Thread3(s), "Thread-3");
t1.start();
t2.start();
t3.start();
}
}
每个线程现在看起来像:
public class Thread1 implements Runnable {
Shared s;
public Thread1(Shared s) {
this.s = s;
}
@Override
public void run() {
int result = s.add(10, 20);
long now= System.currentTimeMillis()-Shared.start;
System.out.println("Sum of 10 and 20 by " + Thread.currentThread().getName() + " is " + result+ " found at "+ now);
}
}
生成以下输出:
Thread-1 calculation has taken place at time 2
Sum of 10 and 20 by Thread-1 is 30 found at 1005
Thread-2 calculation has taken place at time 1005
Sum of 100 and 200 by Thread-2 is 300 found at 2006
Thread-3 calculation has taken place at time 2006
Sum of 1000 and 2000 by Thread-3 is 3000 found at 3007
add()
每个线程都按应有的方式阻塞。
只有稍后显示结果时,下一个线程已经开始计算。
请注意,add()
现在首先是 sysout(很慢),它发生在睡眠期间,这给了它足够的时间。
推荐阅读
- awk - 用特定条件替换列的正负数
- webrtc - 使用 webrtc 的屏幕共享应用程序连接不好
- c++ - std::ranges::elements_view 的逆
- hadoop - 如何获取 Hive 中现有物化视图的 CREATE 查询?
- python - 蟒蛇 3.6.9 。ImportError:没有名为 setuptools_rust 的模块和命令“python setup.py egg_info”失败,错误代码为 1
- c# - 如何将文本插入字符串中的占位符?
- python - 具有英特尔 T265 深度的 OpenCV
- javascript - 在 Vue 中使用 promise,我如何从 API 获取数据?
- python - Python 字典理解 - 不可散列的类型:dict
- javascript - 如何仅在中心显示零网格轴并隐藏图表js中的所有其他网格线