线程中使用的同步方式

  • 本文关键字:同步 方式 线程 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

我不应该得到类似重复序列的东西吗?因为我没有使用synchronized?为什么没有得到如下输出,

线程1得到1线程0得到1威胁1得到3线程0得到3威胁1得到4Thread-0获得4

我在下面使用的代码和Output是按顺序的,没有使用线程安全。

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;
}
}

注意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.使用更多螺纹

在我自己的电脑(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在多个线程中重复。

好吧,我想这只是巧合,在你的情况下,你有一个正确的序列。没有同步就不能保证得到它,例如,对我来说,你的代码已经返回了这个:

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类型和一系列其他因素。查看一个不错的教程,了解有关多线程和并发编程的更多信息

最新更新