怎么暂停和恢复一个线程呢
2017-07-14 · 知道合伙人互联网行家
关注
展开全部
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;
public class test implements Runnable
{
Thread t = new Thread(this);
public static void main(String[] args) {
test t1 = new test();
t1.t.start();
boolean flag = true;
Scanner sc = new Scanner(System.in);
while (true) {
//这里的2改成空格
if(sc.nextLine().equals("2")&&flag)
{
//暂停
t1.t.suspend();
flag = false;
}
if(sc.nextLine().equals("2")&&!flag)
{
//线程恢复
t1.t.resume();
flag = true;
}
}
}
@Override
public void run() {
while (true) {
try {
System.out.println("hello");
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;
public class test implements Runnable
{
Thread t = new Thread(this);
public static void main(String[] args) {
test t1 = new test();
t1.t.start();
boolean flag = true;
Scanner sc = new Scanner(System.in);
while (true) {
//这里的2改成空格
if(sc.nextLine().equals("2")&&flag)
{
//暂停
t1.t.suspend();
flag = false;
}
if(sc.nextLine().equals("2")&&!flag)
{
//线程恢复
t1.t.resume();
flag = true;
}
}
}
@Override
public void run() {
while (true) {
try {
System.out.println("hello");
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询