【发布时间】:2023-03-25 13:32:01
【问题描述】:
public class Test implements Runnable{
private String name;
public Test(String name){
this.name = name;
}
public void run() {
blah(name);
}
public synchronized void blah(String obj) {
System.out.println("Here: "+obj);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Test x = new Test("X");
Test y = new Test("Y");
Thread tx = new Thread(x);
Thread ty = new Thread(y);
tx.start();
ty.start();
}
这个例子应该可以帮助我理解同步,但我没有。这是因为如果我删除单词synchronize,它会打印相同的输出(随机)
【问题讨论】:
-
"这个例子应该可以帮助我理解同步,但我没有。"我们能知道你从哪里得到这个例子吗?
标签: java multithreading concurrency