gpt4 book ai didi

java - Java中的多线程错误

转载 作者:行者123 更新时间:2023-11-29 05:20:54 25 4
gpt4 key购买 nike

我实现了一个简单的线程程序,其中一个线程将元素压入堆栈,另一个线程从堆栈中弹出元素。我得到了一个意外的输出,其中 thread1 将一个元素推送了两次。

import java.io.*;
import java.lang.*;
import java.util.*;

public class Threading_Sample implements Runnable
{
private Thread T1,T2;
String ThreadName="";

Stack<Integer> Stack1=new Stack<Integer>();


public void Push_Element_to_Stack(int element)
{
Stack1.push(element);
}

public void Pop_Element_from_Stack()
{
Stack1.pop();
}

public void run()
{
try
{
//while(Thread.currentThread().isAlive())
for(int i=0;i<10;i++)
{
if(T1.getName().equals("THREAD1"))
{
System.out.println("Current Thread: "+T1.getName());
System.out.println("DOING TASK 1...");

Push_Element_to_Stack(i);
Thread.sleep(100);
System.out.println("Stack1 of Thread1: "+Stack1);
}
if(T2.getName().equals("THREAD2"))
{
System.out.println("Current Thread: "+T2.getName());
System.out.println("DOING TASK 2...");

System.out.println("Stack of Thread2 before: "+Stack1);
Pop_Element_from_Stack();
Thread.sleep(100);
System.out.println("Stack of Thread2 after: "+Stack1);
}
}

}
catch(Exception e)
{
System.out.println("Thread interrupted...");
e.printStackTrace();
}
}

public void start()
{
if(T1==null)
{
T1=new Thread(this,"THREAD1");
T1.start();
try
{
T1.join();
}
catch(Exception e)
{
e.printStackTrace();
}
}
if(T2==null)
{
T2=new Thread(this,"THREAD2");
T2.start();
try
{
T2.join();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

public static void main (String[] args)
{
Threading_Sample TASK1=new Threading_Sample();

TASK1.start();
}
}

输出

--------------------Configuration: <Default>--------------------
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0]
Thread interrupted...
java.lang.NullPointerException
at Threading_Sample.run(Threading_Sample.java:39)
at java.lang.Thread.run(Thread.java:722)
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 0]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 0]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 1]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 1]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 2]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 2]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 3]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 3]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 4]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 4]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 5]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 5]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 6]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 6]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 7]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 7]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 8]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 8]
Stack of Thread2 after: [0]
Current Thread: THREAD1
DOING TASK 1...
Stack1 of Thread1: [0, 9]
Current Thread: THREAD2
DOING TASK 2...
Stack of Thread2 before: [0, 9]
Stack of Thread2 after: [0]

Process completed.

为什么元素0被压入栈两次?

问候,拉杰什。

最佳答案

你想查看当前线程的名称

 Thread.currentThread().getName()

run() 中不是 T1 的名字

无论哪个Thread正在执行它,它总是true

关于java - Java中的多线程错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24816170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com