现有三个类:
1.线程类ExteriorThread
2.调用线程类CallThread
3.停止线程类StopThread流程,当运行CallThread类时,调用线程,不断在后台输出内容;想要停止该线程时,就调用StopThread类。
详情如下:1.ExteriorThread.java
public class ExteriorThread extends Thread{
public static volatile boolean jobCheckStop;
public void run(){
jobCheckStop = false; int i = 0;
while(!jobCheckStop){
System.out.println(i);
i ++ ; try{
Thread.sleep(200);
}catch(Exception e){
Thread.currentThread().interrupt();
}
}
}
}2.CallThread.java
public class CallThread{
public static void main(String[] args){
ExteriorThread et = new ExteriorThread();
et.start();
try{
Thread.sleep(200);
}catch(Exception e){
e.printStackTrace();
}
System.out.println("one");
System.out.println("two");
System.out.println("three");
}
}3.StopThread.java
public class StopThread{
public static void main(String[] args){
ExteriorThread.jobCheckStop = true;
}
}现在情况是,当运行CallThread时,线程正常启动;
但运行StopThread时,线程不停止
哪位知道是什么原因啊?100分全给他
1.线程类ExteriorThread
2.调用线程类CallThread
3.停止线程类StopThread流程,当运行CallThread类时,调用线程,不断在后台输出内容;想要停止该线程时,就调用StopThread类。
详情如下:1.ExteriorThread.java
public class ExteriorThread extends Thread{
public static volatile boolean jobCheckStop;
public void run(){
jobCheckStop = false; int i = 0;
while(!jobCheckStop){
System.out.println(i);
i ++ ; try{
Thread.sleep(200);
}catch(Exception e){
Thread.currentThread().interrupt();
}
}
}
}2.CallThread.java
public class CallThread{
public static void main(String[] args){
ExteriorThread et = new ExteriorThread();
et.start();
try{
Thread.sleep(200);
}catch(Exception e){
e.printStackTrace();
}
System.out.println("one");
System.out.println("two");
System.out.println("three");
}
}3.StopThread.java
public class StopThread{
public static void main(String[] args){
ExteriorThread.jobCheckStop = true;
}
}现在情况是,当运行CallThread时,线程正常启动;
但运行StopThread时,线程不停止
哪位知道是什么原因啊?100分全给他
原因是: 你运行的是两个进程 !!你现运行 CallThread.java 时 是开启了一个ExteriorThread线程,但也启动了一个从main入口的主线程,该主线程也是一个进程,运行后 当你又运行 StopThread.java 又起了一个进程 , 而 *** 进程 ***之间是不可以互相访问的!!! 修改方式如下 : CallThread 类 中最后几行 加
new Thread().sleep(5000);
ExteriorThread.jobCheckStop = true;
即可停止该分线程 ExteriorThread的 run()
你在
import java.io.IOException;
import java.io.InputStreamReader;
import servercontrol.socketserver;public class serverconsole {
static socketserver server;
public serverconsole() {
}
public static void main(String[] arg)
{
tes jj=new tes();
jj.go();
}}
class tes extends Thread {
String hh; tes() {
} public void init(String hh) {
this.hh = hh;
} public void go() {
start();
} public void run() {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
try {
String str = in.readLine();
if(str.equals("start"))
{
serverconsole.server=new socketserver();
serverconsole.server.begin();
}
if(str.equals("stop"))
{
serverconsole.server.end();
}
} catch (IOException ex) {
}
}
}
}
这是我前几天做的一个控制线程停止函数,static socketserver server; 是一个线程
如果你要使用一个类控制线程停止,
if(str.equals("stop"))
{
serverconsole.server.end();
}
需要修改,你直接把server 做为一个参数传到一个类里面,再使用类的方法控制停止
不知道是你需要的吗
return exteriorThread ;
} 在 StopThread 类中作判断,如果 需要 停止该线程
则这样写 ExteriorThread.getExteriorThreadInstance().jobCheckStop = true;但绝对不可以运行两个 main class !!!