package test1;
/**
* @author Macro
* @Creation Time 2013-4-5
* @version v1.0.1
* @function 多线程控制方法
* 1:start():用于开始执行run()方法中定义的线程体
* 2:sleep():调整java运行时间,指定调用线程的睡眠时间
* 3:jion():用于调用线程等待本线程结束
* 4:yield():暂时停止调用线程并将其放在队列末尾,等待另一轮执行,使同一优先级的其他线程有机会运行
*/
class FristThread extends Thread {
public void run() {
System.out.println("First thread starts running");
try {
sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("First thread finishes running");
}
}
class SecondThread extends Thread {
public synchronized void run() {
System.out.println("Second thread starts running");
System.out.println("Second thread suspends running");
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Second thread run again and finishes ");
}
public static void main(String[] args) {
FristThread frist = new FristThread();
SecondThread second = new SecondThread();
frist.start();
second.start();
try {
System.out.println("waiting for first thread to finishing...");
frist.join();
System.out.println("it is a long wait!");
System.out.println("waking up second thread...");
synchronized (second) {
second.notify();
}
System.out.println("waking for second thread to finishing ...");
second.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("i'm ready to finish too.");
}
}
结果:
First thread starts running
waiting for first thread to finishing...
Second thread starts running
Second thread suspends running
First thread finishes running
it is a long wait!
waking up second thread...
waking for second thread to finishing ...
Second thread run again and finishes
i'm ready to finish too.
/**
* @author Macro
* @Creation Time 2013-4-5
* @version v1.0.1
* @function 多线程控制方法
* 1:start():用于开始执行run()方法中定义的线程体
* 2:sleep():调整java运行时间,指定调用线程的睡眠时间
* 3:jion():用于调用线程等待本线程结束
* 4:yield():暂时停止调用线程并将其放在队列末尾,等待另一轮执行,使同一优先级的其他线程有机会运行
*/
class FristThread extends Thread {
public void run() {
System.out.println("First thread starts running");
try {
sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("First thread finishes running");
}
}
class SecondThread extends Thread {
public synchronized void run() {
System.out.println("Second thread starts running");
System.out.println("Second thread suspends running");
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Second thread run again and finishes ");
}
}
public static void main(String[] args) {
FristThread frist = new FristThread();
SecondThread second = new SecondThread();
frist.start();
second.start();
try {
System.out.println("waiting for first thread to finishing...");
frist.join();
System.out.println("it is a long wait!");
System.out.println("waking up second thread...");
synchronized (second) {
second.notify();
}
System.out.println("waking for second thread to finishing ...");
second.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("i'm ready to finish too.");
}
}
结果:
First thread starts running
waiting for first thread to finishing...
Second thread starts running
Second thread suspends running
First thread finishes running
it is a long wait!
waking up second thread...
waking for second thread to finishing ...
Second thread run again and finishes
i'm ready to finish too.