62,623
社区成员
发帖
与我相关
我的任务
分享import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) throws IOException, InterruptedException {
TestThread t = new TestThread();
t.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while (true) {
line = reader.readLine();
System.out.println("line=" + line);
if ("p".equalsIgnoreCase(line)) {
t.pause = true;
} else {
t.pause = false;
synchronized (t) {
t.notifyAll();
}
}
}
}
}
class TestThread extends Thread {
boolean pause;
public void run() {
while (true) {
try {
if (pause) {
synchronized (this) {
wait();
}
}
System.out.println("I am working ...");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class testThread {
public static void main(String[] args) throws InterruptedException {
A a = new A();
a.start();
synchronized(a){
a.wait(10000);
}
synchronized(a){
a.notifyAll();
}
}
}
class A extends Thread{
public void run(){
try {
for(int i=0; i<10; i++)
{
System.out.println(" do ......" + i);
}
} catch (Exception dx) {
dx.printStackTrace();
}
}
}
public class test{
test() throws Exception{
Thread t[] = new Thread[5];
final Thread o = new Thread();
for(int i=0;i<t.length;i++){
final int j = i;
t[i] = new Thread(){
{start();}
public void run(){
try{
System.out.println("my name is "+j);
synchronized(o){
o.wait();
}
run();
}catch(Exception e){System.out.println(e);}
}
};
}
for(int i=0;i<10;i++){
synchronized(this){
wait(500);
}
synchronized(o){
o.notifyAll();
}
}
System.exit(0);
}
public static void main(String args[]) throws Exception{
new test();
}
}