如何在线程中同步使用

时间:2018-11-04 09:17:45

标签: java

在未同步的情况下,我运行了以下代码, 而且我的顺序如下。

Thread-1 got 1
Thread-0 got 2
Thread-1 got 3
Thread-0 got 4
Thread-1 got 5
Thread-0 got 6
Thread-1 got 7
Thread-0 got 8
Thread-1 got 9
Thread-0 got 10
Thread-1 got 11
Thread-0 got 12
Thread-1 got 13
Thread-0 got 14
Thread-1 got 15
Thread-0 got 16
Thread-1 got 17
Thread-0 got 18
Thread-1 got 19
Thread-0 got 20
Thread-1 got 21
Thread-0 got 22
Thread-1 got 23
Thread-0 got 24
Thread-1 got 25
Thread-0 got 26
Thread-1 got 27
Thread-0 got 28
Thread-1 got 29
Thread-0 got 30
Thread-1 got 31
Thread-0 got 32
Thread-1 got 33
Thread-0 got 34
Thread-1 got 35
Thread-0 got 36
Thread-0 got 38
Thread-1 got 37

我不应该使用同步吗? 为什么没有得到如下输出,

线程1得到1 线程-0得到1 线程1得到3 主题0得到了3 线程1得到4 线程0得到了4

我在下面和输出中使用的代码是按顺序进行的,而没有使用线程安全性。

public class T1 {

    public static void main(String[] args) {

        Increment inc = new Increment();

        runI r = new runI(inc);
        r.start();

        runI r1 = new runI(inc);
        r1.start();

    }

}

class runI extends Thread {

    Increment iv = null;

    public runI(Increment iv) {
        this.iv = iv;
    }

    public void run() {

        for (int i = 1; i < 20; i++) {

            System.out.println(Thread.currentThread().getName() + " got " + iv.getNext());
        }

    }

}
public class Increment {

    int value = 0;

    public int getNext() {

        value++;
        return value;

    }

}

2 个答案:

答案 0 :(得分:0)

注意 System.out.println将在PrintStream实例上同步。因此,此行为使多线程将在打印时进行同步。在您的情况下,使并发问题发生的可能性低。

使用以下方法之一轻松产生并发问题:

1。最后只能打印结果

这种方式将减少同步对PrintStream实例的影响。

StringBuilder builder = new StringBuilder();
for (int i = 1; i < 20; i++) {
    builder.append(Thread.currentThread().getName() + " got " + iv.getNext()).append("\n");
}
System.out.println(builder.toString());

代替每次打印:

for (int i = 1; i < 20; i++) {
    System.out.println(Thread.currentThread().getName() + " got " + iv.getNext());
}

2。使用更多线程

在我自己的pc(具有6个内核的cpu)上,使用4个或更多线程将很容易产生并发问题。 以下是4个线程下的结果:

Thread-1 got 1
Thread-2 got 2
Thread-3 got 3
Thread-0 got 1
Thread-3 got 6
Thread-2 got 5
Thread-1 got 4
Thread-2 got 9
Thread-3 got 8
Thread-3 got 12
Thread-3 got 13
Thread-0 got 7
Thread-3 got 14
Thread-3 got 16
Thread-3 got 17
Thread-3 got 18
Thread-2 got 11
Thread-1 got 10
Thread-2 got 20
Thread-3 got 19
Thread-3 got 23
Thread-0 got 15
Thread-3 got 24
Thread-2 got 22
Thread-1 got 21
Thread-1 got 28
Thread-2 got 27
Thread-3 got 26
Thread-3 got 31
Thread-3 got 32
Thread-3 got 33
Thread-0 got 25
Thread-3 got 34
Thread-2 got 30
Thread-1 got 29
Thread-2 got 37
Thread-2 got 39
Thread-3 got 36
Thread-0 got 35
Thread-3 got 41
Thread-2 got 40
Thread-1 got 38
Thread-2 got 43
Thread-0 got 42
Thread-2 got 45
Thread-1 got 44
Thread-1 got 48
Thread-2 got 47
Thread-0 got 46
Thread-2 got 50
Thread-1 got 49
Thread-1 got 53
Thread-1 got 54
Thread-2 got 52
Thread-0 got 51
Thread-2 got 56
Thread-1 got 55
Thread-2 got 58
Thread-2 got 60
Thread-0 got 57
Thread-1 got 59
Thread-0 got 61
Thread-1 got 62
Thread-1 got 64
Thread-0 got 63
Thread-1 got 65
Thread-0 got 66
Thread-0 got 68
Thread-0 got 69
Thread-0 got 70
Thread-0 got 71
Thread-0 got 72
Thread-0 got 73
Thread-0 got 74
Thread-1 got 67
Thread-1 got 75

在这里我们可以看到数字1在多个线程中重复出现。

答案 1 :(得分:0)

嗯,我想这只是一个巧合,在您的情况下,您有一个正确的顺序。如果没有同步,则无法保证获得同步。对我来说,您的代码已返回以下代码:

Thread-0 got 1
Thread-0 got 3
Thread-0 got 4
Thread-1 got 2
Thread-0 got 5
Thread-1 got 6
Thread-0 got 7
Thread-1 got 8
Thread-0 got 9
Thread-1 got 10
Thread-0 got 11
Thread-1 got 12
Thread-0 got 13
Thread-1 got 14
Thread-0 got 15
Thread-0 got 17
Thread-0 got 18
Thread-1 got 16
Thread-0 got 19
Thread-1 got 20
Thread-0 got 21
Thread-1 got 22
Thread-0 got 23
Thread-1 got 24
Thread-0 got 25
Thread-1 got 26
Thread-0 got 27
Thread-1 got 28
Thread-0 got 29
Thread-1 got 30
Thread-0 got 31
Thread-1 got 32
Thread-0 got 33
Thread-1 got 34
Thread-1 got 35
Thread-1 got 36
Thread-1 got 37
Thread-1 got 38

通常,它主要取决于CPU类型和许多其他因素。看看nice tutorial,了解有关多线程和并发编程的更多信息