简体   繁体   English

如何在线程中同步使用

[英]How synchronized used in thread

Without synchronized i ran this below code, and i am getting in sequential order as below. 没有同步,我运行下面的代码,并且我得到如下的顺序。

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

shouldn't i get something like repeated sequence as i am not using synchronized? 我不应该使用重复序列,因为我没有使用同步吗? Why am not getting output such as below, 为什么没有得到如下输出,

Thread-1 got 1 Thread-0 got 1 Thread-1 got 3 Thread-0 got 3 Thread-1 got 4 Thread-0 got 4 线程1得到1线程0得到1线程1得到3线程0得到3线程1得到4线程0得到4

the code i used below and Output is in sequence without using thread safety. 我在下面使用的代码和输出是按顺序进行的,而没有使用线程安全性。

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;

    }

}

Note System.out.println will synchronized on the PrintStream instance. 注意 System.out.println将在PrintStream实例上同步。 So this behavior makes the multi threads will do sync on printing. 因此,此行为使多线程将在打印时进行同步。 In your case, make the concurrent issue at low probability. 在您的情况下,请以较低的概率发出并发问题。

Use one of below ways to produce concurrent issue easily: 使用以下方法之一轻松产生并发问题:

1. only print result at last 1.最后只打印结果

This way will reduce the affect of sync on PrintStream instance. 这种方式将减少同步对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());

instead of printing it each time: 而不是每次都打印:

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

2. Use more threads 2.使用更多线程

On my own pc(cpu with 6 cores), use 4 or more threads will easy produce the concurrent issue. 在我自己的PC(具有6个内核的CPU)上,使用4个或更多线程将很容易产生并发问题。 Below is the result under 4 threads: 以下是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

Here we can see the number 1 repeat in more than one threads. 在这里我们可以看到数字1在多个线程中重复。

Well, I guess it's just a coincidence that in your case you had a right sequence. 好吧,我想这只是一个巧合,在您的情况下,您有一个正确的顺序。 Without synchronization there is no guarantee to get it, eg for me your code has returned this: 如果没有同步,则无法保证会得到它,例如,对我来说,您的代码已返回以下代码:

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

In general it mostly depends on a CPU type and a bunch of another factors. 通常,它主要取决于CPU类型和许多其他因素。 Take a look at a nice tutorial for more information about multithreading and concurrent programming 看看一个不错的教程 ,了解有关多线程和并发编程的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM