同步方法多线程应用程序
Synchronized method multy threading app
我尝试使用同步方法,但我遇到了一些有趣的行为,例如:
我有三个线程 ThreadA - 调用 Increment 方法,ThreadB 调用 Decrement 方法和 ThreadC 打印当前值;
但有些值看起来不正确:
调试:
准备线程
错误的
1
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0....
当它应该全部为 0 和 1 或其他值时;
我的代码如下:
package concurrency;
public class Synchronization {
public static void main(String[] args) throws InterruptedException {
System.out.println("Preparing threads");
Counter cnt = new Counter();
Thread threadA = new Thread(new ThreadA(cnt));
Thread threadB = new Thread(new ThreadB(cnt));
Thread ThreadC = new Thread (new ThreadC(cnt));
threadA.start();
threadB.start();
ThreadC.start();
}
private static class ThreadA implements Runnable {
private final Counter counter;
public ThreadA(Counter cnt) {
counter = cnt;
}
@Override
public void run() {
Thread.currentThread().setName("ThreadA");
System.out.println(((Boolean)
Thread.currentThread().isInterrupted()).toString());
try {
while (!Thread.currentThread().isInterrupted()) {
counter.Increment();
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("InterruptedException ThreadA");
}
System.out.println("ThreadA finished");
}
}
private static class ThreadB implements Runnable {
private final Counter counter;
public ThreadB(Counter cnt) {
counter = cnt;
}
@Override
public void run() {
Thread.currentThread().setName("ThreadB");
try {
while (true) {
if (Thread.currentThread().isInterrupted()) {
break;
}
counter.Decrement();
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("InterruptedExcepion threadB");
}
System.out.println("ThreadB Finished");
}
}
private static class ThreadC implements Runnable {
private final Counter counter;
public ThreadC(Counter cnt) {
counter = cnt;
}
@Override
public void run() {
Thread.currentThread().setName("ThreadC");
try {
while (true) {
if (Thread.currentThread().isInterrupted()) {
break;
}
System.out.println(counter.getValue());
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("InterruptedExcepion threadB");
}
System.out.println("ThreadB Finished");
}
}
private static class Counter {
private int value = 0;
public synchronized void Increment() {
value++;
}
public synchronized void Decrement() {
value--;
}
public synchronized int getValue() {
return value;
}
}
}
不知道你看到这里有什么出乎意料的地方。此代码有 3 个线程使用公共共享计数器。有足够的同步来确保线程不会以无效的方式修改共享数据或看到陈旧的值。但输出会有所不同,具体取决于哪些线程在何时获得更多 CPU 时间。没有要求这些线程 运行 以任何特定顺序排列,哪些线程 运行 以及谁获得锁可能看起来很随意。其输出将不可预测。
我尝试使用同步方法,但我遇到了一些有趣的行为,例如:
我有三个线程 ThreadA - 调用 Increment 方法,ThreadB 调用 Decrement 方法和 ThreadC 打印当前值; 但有些值看起来不正确:
调试: 准备线程 错误的 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0....
当它应该全部为 0 和 1 或其他值时;
我的代码如下:
package concurrency;
public class Synchronization {
public static void main(String[] args) throws InterruptedException {
System.out.println("Preparing threads");
Counter cnt = new Counter();
Thread threadA = new Thread(new ThreadA(cnt));
Thread threadB = new Thread(new ThreadB(cnt));
Thread ThreadC = new Thread (new ThreadC(cnt));
threadA.start();
threadB.start();
ThreadC.start();
}
private static class ThreadA implements Runnable {
private final Counter counter;
public ThreadA(Counter cnt) {
counter = cnt;
}
@Override
public void run() {
Thread.currentThread().setName("ThreadA");
System.out.println(((Boolean)
Thread.currentThread().isInterrupted()).toString());
try {
while (!Thread.currentThread().isInterrupted()) {
counter.Increment();
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("InterruptedException ThreadA");
}
System.out.println("ThreadA finished");
}
}
private static class ThreadB implements Runnable {
private final Counter counter;
public ThreadB(Counter cnt) {
counter = cnt;
}
@Override
public void run() {
Thread.currentThread().setName("ThreadB");
try {
while (true) {
if (Thread.currentThread().isInterrupted()) {
break;
}
counter.Decrement();
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("InterruptedExcepion threadB");
}
System.out.println("ThreadB Finished");
}
}
private static class ThreadC implements Runnable {
private final Counter counter;
public ThreadC(Counter cnt) {
counter = cnt;
}
@Override
public void run() {
Thread.currentThread().setName("ThreadC");
try {
while (true) {
if (Thread.currentThread().isInterrupted()) {
break;
}
System.out.println(counter.getValue());
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("InterruptedExcepion threadB");
}
System.out.println("ThreadB Finished");
}
}
private static class Counter {
private int value = 0;
public synchronized void Increment() {
value++;
}
public synchronized void Decrement() {
value--;
}
public synchronized int getValue() {
return value;
}
}
}
不知道你看到这里有什么出乎意料的地方。此代码有 3 个线程使用公共共享计数器。有足够的同步来确保线程不会以无效的方式修改共享数据或看到陈旧的值。但输出会有所不同,具体取决于哪些线程在何时获得更多 CPU 时间。没有要求这些线程 运行 以任何特定顺序排列,哪些线程 运行 以及谁获得锁可能看起来很随意。其输出将不可预测。