线程的意外行为



我正在努力实现线程2应该首先完成,然后是线程1。为此,我使用join()方法。但是,如果我取消注释thread1类的try块中存在的System.out.println()。然后代码给出空指针异常。为什么在try块中我需要添加行,添加行代码开始工作毫无意义。

演示类

public class Demo {
public static void main(String[] args) throws InterruptedException {
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
t1.start();
t2.start();
System.out.println("main Thread");
Thread.sleep(10);
}
}

Thread1级

public class Thread1 extends Thread {
@Override
public void run() {
try {
//            System.out.println(); // on adding anyline, this whole code works!!, uncommenting this line of code give NPE
Thread2.fetcher.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 5; i++) {
System.out.println("in thread1 class, Thread-1 ");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

Thread2类

public class Thread2 extends Thread {
static Thread fetcher;
@Override
public void run() {
fetcher= Thread.currentThread(); // got the thread2
for (int i = 0; i < 5; i++) {
System.out.println("in thread2 class, Thread-2");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

程序的输出

in thread2 class Thread-2
Exception in thread "Thread-0" java.lang.NullPointerException
at org.tryout.Thread1.run(Thread1.java:22)
in thread2 class Thread-2
in thread2 class Thread-2
in thread2 class Thread-2
in thread2 class Thread-2

它完全由"纯粹的运气">

System.out.println();

内部调用synchronized,它作为一个延迟工作,为提供足够的时间给Thread 2及其字段fetcher

fetcher= Thread.currentThread(); // got the thread2

为了避免这种竞争条件,您需要确保Thread 2Thread 1访问字段fetcher之前设置该字段。为此,您可以使用CyclicBarrier等。

??允许一组线程全部等待的同步辅助工具彼此到达一个共同的障碍点。**CyclicBarriers很有用在涉及固定大小的线程组的程序中偶尔相互等待。屏障被称为循环屏障,因为它可以在等待线程释放后重新使用。

首先,为将调用它的线程数创建一个屏障,即2个线程:

CyclicBarrier barrier = new CyclicBarrier(2);

使用CyclicBarrier,您可以强制Thread 1在访问其字段fetcher:之前等待Thread 2

try {
barrier.await(); // Let us wait for Thread 2.
Thread2.fetcher.join();
} catch (InterruptedException | BrokenBarrierException e) {
// Do something 
}

Thread 2也在设置了字段fetcher之后调用屏障,相应地:

fetcher = Thread.currentThread(); // got the thread2
try {
barrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}

一旦两个线程都调用了屏障,它们就会继续工作。

一个例子:

public class Demo {
public static void main(String[] args) throws InterruptedException             { 
CyclicBarrier barrier = new CyclicBarrier(2);
Thread1 t1 = new Thread1(barrier);
Thread2 t2 = new Thread2(barrier);
t1.start();
t2.start();
System.out.println("main Thread");
Thread.sleep(10);
}
}
public class Thread1 extends Thread {
final CyclicBarrier barrier;
public Thread1(CyclicBarrier barrier){
this.barrier = barrier;
}
@Override
public void run() {
try {
barrier.await();
Thread2.fetcher.join();
} catch (InterruptedException | BrokenBarrierException e) {
// Do something 
}
for (int i = 0; i < 5; i++) {
System.out.println("in thread1 class, Thread-1 ");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Thread2 extends Thread {
static Thread fetcher;
final CyclicBarrier barrier;
public Thread2(CyclicBarrier barrier){
this.barrier = barrier;
}
@Override
public void run() {
fetcher = Thread.currentThread(); // got the thread2
try {
barrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
for (int i = 0; i < 5; i++) {
System.out.println("in thread2 class, Thread-2");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

如果您的代码不是用于教育目的,并且您没有被强制使用任何特定的同步机制用于学习目的。在当前上下文中,您可以简单地将thread 2作为thread 1的参数传递,并直接对其调用join,如下所示:

public class Demo {
public static void main(String[] args) throws InterruptedException {
Thread2 t2 = new Thread2();
Thread1 t1 = new Thread1(t2);
t1.start();
t2.start();
System.out.println("main Thread");
Thread.sleep(10);
}
}
public class Thread1 extends Thread {
final Thread thread2;
public Thread1(Thread thread2){
this.thread2 = thread2;
}
@Override
public void run() {
try {
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 5; i++) {
System.out.println("in thread1 class, Thread-1 ");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Thread2 extends Thread {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("in thread2 class, Thread-2");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} 

这应该允许您的代码正常工作。线程启动之间的时间不足,无法初始化fletcher

try {

Thread.sleep(500);

Thread2.fetcher.join();
}  catch (InterruptedException ie) {
} 

对于这么简单的事情,睡眠应该起作用。但对于更复杂的线程,适当的同步是关键。您应该意识到,线程编程可能是编程中最难调试的方面之一。

最新更新