顺序线程执行打印 1A 2B 3C 4D
Sequential thread execution to print 1A 2B 3C 4D
我实现了两个线程,一个用于打印整数,另一个用于打印英文字母。这些线程应该协同工作。
我使用同步块,一次只有一个线程持有锁。但不知何故,AtomicInteger 没有按预期工作。一个线程对计数变量所做的更改对其他不同的线程不可见。这怎么可能。我错过了什么吗?
我的实现
package com.concurrency;
进口java.util.concurrent.atomic.AtomicInteger;
public class ThreadWaitEg
{
// Single monitor object for both threads
private final Object monitor = new Object();
// Thread-safe counter for both threads
private final AtomicInteger count = new AtomicInteger(0);
private class PrintNumber implements Runnable
{
@Override
public void run()
{
for (int i = 0; i < 26; i++)
{
synchronized (monitor)
{
System.out.println("From PrintNumber "+count.get());
try
{
while ((count.get()) % 2 != 0)
{
monitor.wait();
}
} catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(i);
count.getAndIncrement();
monitor.notifyAll();
}
}
}
}
private class PrintChar implements Runnable
{
@Override
public void run()
{
for (int charr = 'A'; charr <= 'Z'; charr++)
{
synchronized (monitor)
{
System.out.println("From PrintChar "+count.get());
try
{
while ((count.get()) % 2 == 0)
{
monitor.wait();
}
} catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println((char) charr);
count.getAndIncrement();
monitor.notifyAll();
}
}
}
}
public static void main(String[] args)
{
System.out.println("from main thread");
Runnable runInt = new ThreadWaitEg().new PrintNumber();
new Thread(runInt).start();
Runnable runChar = new ThreadWaitEg().new PrintChar();
new Thread(runChar).start();
System.out.println(" main thread completes ");
}
}
输出
来自主线程
来自 PrintNumber 0
0
来自 PrintNumber 1
主线程完成
来自 PrintChar 0
您的主要问题是您创建了主 ThreadWaitEg class 的两个实例 - 每个实例都有自己的 "monitor" 和 "count" 对象副本,因此对象不共享在两个线程之间。最快的解决方法是只创建一个实例,即将 main() 方法替换为:
public static void main(String[] args) {
System.out.println("from main thread");
ThreadWaitEg threadWaitEg = new ThreadWaitEg();
Runnable runInt = threadWaitEg.new PrintNumber();
new Thread(runInt).start();
Runnable runChar = threadWaitEg.new PrintChar();
new Thread(runChar).start();
System.out.println(" main thread completes ");
}
我实现了两个线程,一个用于打印整数,另一个用于打印英文字母。这些线程应该协同工作。
我使用同步块,一次只有一个线程持有锁。但不知何故,AtomicInteger 没有按预期工作。一个线程对计数变量所做的更改对其他不同的线程不可见。这怎么可能。我错过了什么吗?
我的实现
package com.concurrency;
进口java.util.concurrent.atomic.AtomicInteger;
public class ThreadWaitEg {
// Single monitor object for both threads
private final Object monitor = new Object();
// Thread-safe counter for both threads
private final AtomicInteger count = new AtomicInteger(0);
private class PrintNumber implements Runnable
{
@Override
public void run()
{
for (int i = 0; i < 26; i++)
{
synchronized (monitor)
{
System.out.println("From PrintNumber "+count.get());
try
{
while ((count.get()) % 2 != 0)
{
monitor.wait();
}
} catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(i);
count.getAndIncrement();
monitor.notifyAll();
}
}
}
}
private class PrintChar implements Runnable
{
@Override
public void run()
{
for (int charr = 'A'; charr <= 'Z'; charr++)
{
synchronized (monitor)
{
System.out.println("From PrintChar "+count.get());
try
{
while ((count.get()) % 2 == 0)
{
monitor.wait();
}
} catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println((char) charr);
count.getAndIncrement();
monitor.notifyAll();
}
}
}
}
public static void main(String[] args)
{
System.out.println("from main thread");
Runnable runInt = new ThreadWaitEg().new PrintNumber();
new Thread(runInt).start();
Runnable runChar = new ThreadWaitEg().new PrintChar();
new Thread(runChar).start();
System.out.println(" main thread completes ");
}
}
输出
来自主线程
来自 PrintNumber 0
0
来自 PrintNumber 1
主线程完成
来自 PrintChar 0
您的主要问题是您创建了主 ThreadWaitEg class 的两个实例 - 每个实例都有自己的 "monitor" 和 "count" 对象副本,因此对象不共享在两个线程之间。最快的解决方法是只创建一个实例,即将 main() 方法替换为:
public static void main(String[] args) {
System.out.println("from main thread");
ThreadWaitEg threadWaitEg = new ThreadWaitEg();
Runnable runInt = threadWaitEg.new PrintNumber();
new Thread(runInt).start();
Runnable runChar = threadWaitEg.new PrintChar();
new Thread(runChar).start();
System.out.println(" main thread completes ");
}