我有一个方法,我想通过调用方法来运行线程。但我可以运行一个线程。但现在我的问题是我想我的线程运行,即使我多次调用该方法。例如,我调用一个调用线程方法的方法,它开始运行。现在我再次调用方法,这不应该导致线程首先运行,而不会影响以前运行的线程。我想过使用Thread.isAlive();.但仍然卡住。如何让线程运行没有任何中断
类MyRunnableThread实现Runnable {
public static int myCount = 0;
public MyRunnableThread(){
}
public void run() {
while(MyRunnableThread.myCount <= 10){
try{
System.out.println("Expl Thread: "+(++MyRunnableThread.myCount));
Thread.sleep(100);
} catch (InterruptedException iex) {
System.out.println("Exception in thread: "+iex.getMessage());
}
}
}
}
public class RunMyThread {
public void call(){
System.out.println("Starting Main Thread...");
MyRunnableThread mrt = new MyRunnableThread();
Thread t = new Thread(mrt);
t.start();
while(MyRunnableThread.myCount <= 10){
try{
System.out.println("Main Thread: "+(++MyRunnableThread.myCount));
Thread.sleep(100);
} catch (InterruptedException iex){
System.out.println("Exception in main thread: "+iex.getMessage());
}
}
System.out.println("End of Main Thread...");
}
}
public void CallThreadCaller()
{
call();
}
现在我打电话是()的调用方法再次 它应该不会影响之前的线程运行。它应该检查它的运行或它应该启动线程。但是,如果我们启动一个新的实例,如何检查一个线程是否已经在运行。
你有没有试过这方面的东西。你能向我们展示你试过的示例代码吗? –
为每个线程创建一个您需要的类的实例,让每个线程与它自己的类 – MadProgrammer