不同线程同步不同对象
Different threads synchronizing on different objects
请看这段代码
public class Test extends Thread {
int i;
public Test(int i) {this.i = i;}
void simpleBlock() throws InterruptedException {
System.out.println(i + " this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.");
synchronized(this) {wait();}
}
public void run() {
try {simpleBlock();} catch (InterruptedException e) {}
}
}
这是由 Main class 实现的,它创建并启动线程
public class Main {
public static void main(String[] args) throws InterruptedException {
Test[] t = new Test[20];
for (int i=0; i<20; i++) {
t[i] = new Test(i);
t[i].start();
}
}
}
这将打印以下输出
0 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
6 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
4 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
3 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
5 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
2 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
1 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
14 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
7 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
13 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
12 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
10 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
11 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
9 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
8 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
18 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
19 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
17 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
16 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
15 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
由于 SimpleBlock()
不同步,我希望印刷品被切碎 运行domly。至少,这就是我 运行 不久前的另一个同步测试所发生的情况,除了那次我使用信号量(或没有使用信号量)。
那么为什么每个线程都以如此有序的方式打印完整的字符串?
这将引导我进行下一个查询。
假设simpleBlock
完全同步,输出同上。所以,
synchronized void simpleBlock() throws InterruptedException {
System.out.println(i + " this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.");
wait();
}
您可能已经知道,这等同于
void simpleBlock() throws InterruptedException {
synchronized(this) {
System.out.println(i + " this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.");
this.wait();
}
}
我的假设是否正确,因为在上面的 class 中创建了 20 个线程,没有两个线程在共享对象上同步,因为每个线程都是它自己的唯一对象,正因为如此,同步方案会有效地失败吗?
换句话说,创建多个线程是不是一个坏主意,每个线程都自己同步?
PrintStream.println
是 synchronized
所以你的输出相对有序,即使你有很多线程。
来源
/**
* Prints a String and then terminate the line. This method behaves as
* though it invokes <code>{@link #print(String)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x The <code>String</code> to be printed.
*/
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
请看这段代码
public class Test extends Thread {
int i;
public Test(int i) {this.i = i;}
void simpleBlock() throws InterruptedException {
System.out.println(i + " this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.");
synchronized(this) {wait();}
}
public void run() {
try {simpleBlock();} catch (InterruptedException e) {}
}
}
这是由 Main class 实现的,它创建并启动线程
public class Main {
public static void main(String[] args) throws InterruptedException {
Test[] t = new Test[20];
for (int i=0; i<20; i++) {
t[i] = new Test(i);
t[i].start();
}
}
}
这将打印以下输出
0 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
6 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
4 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
3 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
5 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
2 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
1 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
14 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
7 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
13 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
12 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
10 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
11 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
9 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
8 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
18 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
19 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
17 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
16 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
15 this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.
由于 SimpleBlock()
不同步,我希望印刷品被切碎 运行domly。至少,这就是我 运行 不久前的另一个同步测试所发生的情况,除了那次我使用信号量(或没有使用信号量)。
那么为什么每个线程都以如此有序的方式打印完整的字符串?
这将引导我进行下一个查询。
假设simpleBlock
完全同步,输出同上。所以,
synchronized void simpleBlock() throws InterruptedException {
System.out.println(i + " this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.");
wait();
}
您可能已经知道,这等同于
void simpleBlock() throws InterruptedException {
synchronized(this) {
System.out.println(i + " this is an example of a thread blocking on itself - practicing concurrency 101, monitors, etc.");
this.wait();
}
}
我的假设是否正确,因为在上面的 class 中创建了 20 个线程,没有两个线程在共享对象上同步,因为每个线程都是它自己的唯一对象,正因为如此,同步方案会有效地失败吗?
换句话说,创建多个线程是不是一个坏主意,每个线程都自己同步?
PrintStream.println
是 synchronized
所以你的输出相对有序,即使你有很多线程。
来源
/**
* Prints a String and then terminate the line. This method behaves as
* though it invokes <code>{@link #print(String)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x The <code>String</code> to be printed.
*/
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}