我试图阻止来自不同类的java线程,但无法弄清楚。我查看了下面的链接,从过去的2天搜索了很多,但无法确定。可能是一个简单的事情,我需要改变,但我没有选择,因此在这里发布。
推荐链接
在输入问题时,我也提到了以下链接..
Stop a thread from outside
以下是我的代码示例。我可以从WorkerThread
启动MainThread
并进入循环。但是无法阻止线程开始使用StopThread
类。
我还使用了以下链接中建议的volatile
选项
http://tutorials.jenkov.com/java-concurrency/volatile.html
我觉得我犯了一个简单的错误,但却无法识别它。
//class WorkerThread
package main;
public class WorkerThread implements Runnable
{
public WorkerThread() {
isRunning = true;
}
public WorkerThread(boolean False) {
isRunning = False;
}
private volatile boolean isRunning;
public synchronized void stopThread() {
isRunning = false;
}
public synchronized boolean IsThreadRunning() {
return isRunning;
}
@Override
public void run()
{
int i = 1;
while(isRunning)
{
System.out.println("Loop " + i);
i++;
try { Thread.sleep(2000); }
catch (InterruptedException e) { e.printStackTrace(); }
}
}
}
//class MainThread
package main;
public class MainThread
{
public static Thread t;
public static void main(String[] args)
{
t = new Thread(new WorkerThread());
t.start();
}
}
//class StopThread
package main;
public class StopThread
{
public static void main(String[] args)
{
//What should i write here to stop the thread started by MainThread
MainThread.t.interrupt();
}
}
答案 0 :(得分:1)
public class MainThread
{
public static Thread t;
public static void main(String[] args)
{
t = new Thread(new WorkerThread());
t.start();
}
}
public class StopThread
{
public static void main(String[] args)
{
MainThread.t.interrupt();
}
}
调用Thread.stop()
在JavaDocs中列为已弃用
这也可能仅仅是为了这个问题,但为什么你的程序有两个主要方法?
答案 1 :(得分:0)
让线程成为MainThread类的公共成员,然后从StopThread调用MainThread.t.interrupt()
答案 2 :(得分:0)
您有机会利用您定义的volatile变量,并优雅地从以下线程中脱颖而出:
public class MainThread
{
public static WorkerThread workerThread;
public static void main(String[] args)
{
workerThread = new WorkerThread();
Thread t = new Thread(workerThread);
t.start();
}
}
public class StopThread
{
public static void main(String[] args)
{
Main.workerThread.stopThread();
}
}
答案 3 :(得分:0)
注意:此解决方案有效但不是完美的解决方案。
您可以从属性文件中写入和读取isRunning
变量的值。这样,您就可以在两个不同的Java进程之间进行交互。 ThreadWorker只是在启动时创建文件&然后尝试在此之后读取文件。 StopThread在触发时修改属性文件,应由ThreadWorker选取。
检查以下示例:
public class ThreadWorker implements Runnable
{
public volatile static boolean isRunning = false;
public ThreadWorker() {
Properties p = new Properties();
p.setProperty("isRunning", "1");
FileOutputStream out;
try {
//Writes all properties in appProperties file
out = new FileOutputStream("appProperties");
p.store(out, "---Thread Status----");
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run()
{
int i = 1;
String status = "1";
while("1".equals(status))
{
status = getStatus();
System.out.println("Loop " + i);
i++;
try { Thread.sleep(2000); }
catch (InterruptedException e) { e.printStackTrace(); }
}
}
public String getStatus() {
FileInputStream in;
Properties p = new Properties();
try {
in = new FileInputStream("appProperties");
p.load(in);
return p.getProperty("isRunning");
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//class StopThread
public class StopThread
{
public static void main(String[] args)
{
Properties p = new Properties();
p.setProperty("isRunning", "0");
FileOutputStream out;
try {
out = new FileOutputStream("appProperties");
p.store(out, "---Thread Status----");
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//class StopThread
public class StopThread
{
public static void main(String[] args)
{
Properties p = new Properties();
p.setProperty("isRunning", "0");
FileOutputStream out;
try {
out = new FileOutputStream("appProperties");
p.store(out, "---Thread Status----");
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}