首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在不同对象上同步的不同线程

在不同对象上同步的不同线程
EN

Stack Overflow用户
提问于 2017-04-13 08:25:07
回答 1查看 53关注 0票数 0

请看这段代码

代码语言:javascript
复制
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) {}
    }
}

这是由创建和启动线程的主类实现的。

代码语言:javascript
复制
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();
        }
    }
}

这将打印以下输出

代码语言:javascript
复制
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()是不同步的,所以我希望这些打印文件会被随机剪切。至少,这是我在一段时间前运行的另一个同步测试中发生的情况,但当时我使用的是信号量(或者没有信号量)。

,那么为什么每个线程都以如此有序的方式打印完整的字符串呢?

这就引出了我的下一个调查。

假设simpleBlock是完全同步的,并且输出与上面相同。所以,

代码语言:javascript
复制
    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();
    }

您可能已经知道,这相当于

代码语言:javascript
复制
    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();
        }
    }

我是否正确地假设,由于在上面的主类中创建了20个线程,没有两个线程在共享对象上同步,因为每个线程都是它自己的唯一对象,因此同步方案实际上会失败吗?

换句话说,创建多个线程(每个线程在自身上同步)是不是一个坏主意?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-13 08:30:05

PrintStream.printlnsynchronized,所以即使有很多线程,输出也是相对有序的。

从源头

代码语言:javascript
复制
/**
 * 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();
    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43387098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档