(1)sleep()
使当前线程暂停执行一段时间, 让其他线程有机会继续执行,但是并不释放对象锁,如果有synchronized同步块,其他线程仍然不能共享数据。注意该方法需要捕获异常。
假设两个线程同时执行(没有sychronized同步代码块),一个线程优先级为MAX_PRIORITY(线程优先级别,从1到10,最高为10),另一个为MIN_PRIORITY,如果没有sleep()方法,线程将先执行级别高的线程,后执行级别低的线程,假如级别高线程sleep(),则低线程将有机会执行。当然,sleep()也可以让同级或者高级别线程有执行机会。
package main.com;
/**
* Created by lulei on 2017/7/11.
*/
public class ThreadPriority {
public static void main(String[] args) {
Thread producer = new Producer();
Thread consumer = new Consumer();
producer.setPriority(Thread.MIN_PRIORITY);//Thread.MIN_PRIORITY指线程执行的优先级,从1到10,最低为1,最高为10.
consumer.setPriority(Thread.MAX_PRIORITY);
producer.start();
consumer.start();
}
}
class Producer extends Thread{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("I am a Producer:Item "+i);
// Thread.yield();
}
}
}
class Consumer extends Thread{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("I am a Consumer:Item "+i);
// Thread.yield();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
如果将Consumer中的sleep()方法注掉,执行结果
加上以后
(2)join()方法
join()方法使调用该方法的线程在此之前执行完毕,也就是等待该线程的方法执行完毕以后再往下继续执行,该方法也需要捕获异常。
package main.com;
/**
* Created by lulei on 2017/7/11.
*/
public class JoinDemo {
public static void main(String[] args) {
Thread t = new Thread(new RunableImpl());
t.start();
try {
t.join(1000);
System.out.println("joinFinish");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class RunableImpl implements Runnable{
@Override
public void run() {
try {
System.out.println("Thread Begin");
Thread.sleep(500);
System.out.println("end Sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
执行结果:
Thread Begin
end Sleep
joinFinish
如果t线程sleep 2000呢,执行结果是
Thread Begin
joinFinish
end Sleep
这就说明,无论t线程sleep多久,t.join()只等待1000毫秒
join的JDK中源码如下
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
从这里,可以看到,join()方法的实现是通过wait实现的,也就是说,当main线程调用t.join()的时候,main线程会获得线程对象t的锁(wait就是拿到该对象的锁),调用该对象的wait,直到该对象唤醒main线程。这就意味着,main线程调用t.join()时,必须能够拿到t线程对象的锁,如果拿不到它是无法wait的,假如在它等待之前,有其他的线程获得t对象的锁,它等待的时间可就不止1000毫秒了。
join方法的作用:
也就是主线程等待子线程的终止。也就是在子线程调用了join方法后面的代码,就是不管子线程什么时候结束,main线程只等待子线程对应设定的时间。
(3)sleep()方法和wait()方法
sleep方法和wait方法区别,首先在sleep是属于Thread类中的方法,wait是属于Object类中的方法。
sleep导致程序暂时停止执行,让出CPU给其他线程,但是它的监控状态一直保持,当指定时间到了又会自动恢复运行状态。在sleep调用的时候,不会释放对象锁。调用wait方法的时候,线程会放弃对象锁,进入等待此对象的锁定线程池,只有针对此对象调用notify方法后,该线程才进入对象锁定池准备,获取对象后进入运行状态。
(4)notify()和notifyAll()方法
都是object类中的方法。
notify是唤醒某一个线程,只有一个线程获得资源对象锁
notifyAll是唤醒所有的线程,所有的线程会竞争获取资源对象锁,只能有一个获取,然后执行。