多线程的利用在进行进程调度中具有重要的意义,是java的一个比较有意义的功能,它实现了时间和空间上的合理分配。以下例子实现了两个线程的随时单独暂停和继续,多个线程只要往上加就可以了,代码如下:
public class TestThread implements Runnable {
public int state1 = 0;
public int state = 0;
public String name;
public TestThread(String name) {
// new Thread(this).start();
this.name = name;
}
@Override
public void run() {
// state为2时,进入等待,等到state1=1时的唤醒后继续
synchronized (this) {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (2 == state) {
System.out.println(this.state);
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(this.name + " Run--------------->");
}
}
}
public void wakeUpThisThread() {
synchronized (this) {
if (1 == this.state1) {
this.notify();
}
}
}
public static void main(String[] args) {
//启动两个线程做对比
TestThread tt1 = new TestThread("Thread1");
Thread t1 = new Thread(tt1);
t1.start();
// 第二段
TestThread tt2 = new TestThread("Thread2");
Thread t2 = new Thread(tt2);
t2.start();
//5秒钟后看线程暂停
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tt1.state = 2;
tt2.state = 2;
//8秒后只恢复线程1,23秒后恢复线程2,可见notify并不是针对所有的,是可以单独针对某线程的
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("激活");
tt1.state1 = 1;
tt1.state = 0;
tt1.wakeUpThisThread();
tt2.state1 = 1;
tt2.state = 0;
// tt2.wakeUpThisThread();
int count=0;
while (true) {
count++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(15==count){
tt2.wakeUpThisThread();
}
}
}
}
public class TestThread implements Runnable {
public int state1 = 0;
public int state = 0;
public String name;
public TestThread(String name) {
// new Thread(this).start();
this.name = name;
}
@Override
public void run() {
// state为2时,进入等待,等到state1=1时的唤醒后继续
synchronized (this) {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (2 == state) {
System.out.println(this.state);
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(this.name + " Run--------------->");
}
}
}
public void wakeUpThisThread() {
synchronized (this) {
if (1 == this.state1) {
this.notify();
}
}
}
public static void main(String[] args) {
//启动两个线程做对比
TestThread tt1 = new TestThread("Thread1");
Thread t1 = new Thread(tt1);
t1.start();
// 第二段
TestThread tt2 = new TestThread("Thread2");
Thread t2 = new Thread(tt2);
t2.start();
//5秒钟后看线程暂停
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tt1.state = 2;
tt2.state = 2;
//8秒后只恢复线程1,23秒后恢复线程2,可见notify并不是针对所有的,是可以单独针对某线程的
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("激活");
tt1.state1 = 1;
tt1.state = 0;
tt1.wakeUpThisThread();
tt2.state1 = 1;
tt2.state = 0;
// tt2.wakeUpThisThread();
int count=0;
while (true) {
count++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(15==count){
tt2.wakeUpThisThread();
}
}
}
}