1.sleep()和wait()
这两个方法都可以让调用它的线程沉睡(sleep)/停止运行(wait)指定的时间,到了这个时间,线程就会自动醒来,变为可运行状态(RUNNABLE)。
public static native void sleep(long millis) throws InterruptedException;
public static void sleep(long millis, int nanos) throws InterruptedException
public final void wait() throws InterruptedException
public final native void wait(long timeout) throws InterruptedException;
public final void wait(long timeout, int nanos) throws InterruptedException
Parameters:
millis - the length of time to sleep in milliseconds.毫秒数
nanos - 0-999999 additional nanoseconds to sleep.纳秒数
调用sleep()方法并不会让线程释放它所持有的同步锁;而且在这期间它也不会阻碍其它线程的运行。
当调用了某个对象的wait()方法时,当前运行的线程就会转入WAITING状态,等待别的线程再次调用这个对象的notify()或者notifyAll()方法唤醒它,或者到了指定的最大等待时间,线程自动醒来。如果线程调用了某个对象的wait()方法,这个线程就会释放这个对象所持有的同步资源(不会释放其他对象的同步锁)。jdk api doc:The current thread must own this object‘s monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object‘s monitor to wake up either through a call to the notify method or the notifyAll method
- package edu.hust.test;
- public class ThreadSleep implements Runnable {
-
- public void execute() {
- synchronized(this) {
- try {
- System.out.println(Thread.currentThread().getName() + ", sleep()前");
- Thread.sleep(1000);
- System.out.println(Thread.currentThread().getName() + ", sleep()后");
- } catch (InterruptedException e) {
- System.out.println(Thread.currentThread().getName() + ", 谁把我吵醒了.....");
- }
-
- System.out.println(Thread.currentThread().getName() + ", run()结束..进入TERMINATED状态");
- }
- }
-
- public void run() {
- execute();
- }
-
- public static void main(String[] args) throws InterruptedException {
- ThreadSleep threadSleep = new ThreadSleep();
- Thread[] threads = new Thread[5];
-
- System.out.println(Thread.currentThread().getName() + "线程的状态为:" + Thread.currentThread().getState());
-
- for (Thread thread : threads) {
- thread = new Thread(threadSleep);
- thread.start();
- if ("Thread-1".equals(thread.getName()) || "Thread-3".equals(thread.getName()))
- thread.interrupt();
- }
- }
-
- }
- package edu.hust.test;
- class MyThread1 implements Runnable {
- private Object obj;
- public MyThread1(Object o) {
- obj = o;
- }
-
- public void run() {
- synchronized (obj) {
- try {
- System.out.println("MyThread1进入wait状态");
- obj.wait();
- System.out.println("MyThread1被notify");
- } catch (InterruptedException e) {
- System.err.println("谁把我吵醒了.....");
- }
- }
- }
- }
- class MyThread2 implements Runnable {
- private Object obj;
- public MyThread2(Object o) {
- obj = o;
- }
-
- public void run() {
- synchronized (obj) {
- System.out.println("MyThread2调用notify()方法");
- obj.notify();
- }
- }
- }
- public class ThreadWait {
- public static void main(String[] args) {
-
-
-
-
-
- String str = "爱吃土豆";
- Thread myThread1 = new Thread(new MyThread1(str));
- Thread myThread2 = new Thread(new MyThread2(str));
- myThread1.start();
- myThread2.start();
- }
-
-
-
-
-
- }
- package edu.hust.test;
- public class ThreadWait2 implements Runnable {
-
- private Object monitor1 = new Object();
- private Object monitor2 = new Object();
-
- public void run() {
- synchronized (monitor1) {
- System.out.println("monitor1被锁住了");
- synchronized (monitor2) {
- System.out.println("monitor2被锁住了");
- try {
- System.out.println("monitor2进入wait()状态");
- monitor2.wait();
- } catch (InterruptedException e) {
- System.out.println("谁把我吵醒了.....");
- }
- }
- }
- }
-
- public void getMonitor() throws InterruptedException {
- Thread.sleep(3 * 1000);
- synchronized (monitor2) {
- System.out.println("我取得了monitor2");
- }
-
- synchronized (monitor1) {
- System.out.println("我取得了monitor1");
- }
- }
-
- public static void main(String[] args) {
- ThreadWait2 threadWait2 = new ThreadWait2();
- Thread myThread = new Thread(threadWait2);
- myThread.start();
- try {
- threadWait2.getMonitor();
- } catch (InterruptedException e) {
- System.out.println("谁把我吵醒了.....");
- }
- }
-
- }
2. yield()
让当前运行Thread放弃其所占用的cpu时间片,以便让其他Thread运行。用yield()方法的目的是让Thread能适当地轮转。但是,并不能保证达到此效果!因为,即使当前Thread放弃时间片,可是还有可能再次被JVM选中!也就是连任。
3. join()
- package edu.hust.test;
- public class ThreadJoin {
- public void run() {
- System.out.println("普通打印语句1");
- System.out.println("普通打印语句2");
- }
-
- public static void main(String[] args) throws InterruptedException {
- Thread myThread = new Thread() {
- public void run() {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- System.err.println("谁把我吵醒了.....");
- }
- System.out.println("线程:" + Thread.currentThread().getName() + " 启动了");
- }
- };
- myThread.start();
- myThread.join();
-
- new ThreadSeq().run();
- }
-
- }
thread相关http://blog.csdn.net/forwayfarer/article/details/3455130
原文:http://www.cnblogs.com/kimmychul/p/3941990.html