java 线程 控制_Java并发编程:线程控制

在上一篇文章中(Java并发编程:线程的基本状态)我们介绍了线程状态的 5 种基本状态以及线程的声明周期。这篇文章将深入讲解Java如何对线程进行状态控制,比如:如何将一个线程从一个状态转到另一个状态,如何设置线程的优先级等。

1326fe9baef02cf510ab9bc2b60252bd.png

一、join()   等待阻塞

让一个线程等待另一个线程完成才继续执行。如A线程线程执行体中调用B线程的join()方法,则A线程被阻塞,知道B线程执行完为止,A才能得以继续执行。

packagecom.chanshuyi.thread;public classThreadDemo6 {public static voidmain(String[] args) {//I'm the thread.//Main Thread is Running over.

Thread thread = newThread(){

@Overridepublic voidrun(){

System.out.println("I'm the thread.");try{

Thread.sleep(2000); //让其休眠2秒,测试主线程是否等待线程执行完再执行

}catch(Exception e){

e.printStackTrace();

}

}

};

thread.start();try{

thread.join();//让main线程等待线程执行完再执行

} catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("Main Thread is Running over.");

}

}

代码中我特意让子线程休眠了2秒,但是最终的结果还是main线程等待子线程运行后再继续运行。

二、wait()/notify()   锁阻塞

wait() 和 notify() 方法的调用需要调用的方法有一个锁对象,主要用于进行不同线程之间的同步协作,常见的有生产者消费者模型。

packagecom.chanshuyi.thread;/*** 生产者消费者模型

*@authorAdministrator

**/

public classThreadDemo92 {public static voidmain(String[] args) {final ProduceConsumer pc = newProduceConsumer();//生产者线程

newThread(){

@Overridepublic voidrun(){//生产10次

for(int i = 0; i < 5; i++){

System.out.println("Ready to Produce:" + (i + 1));

pc.produce(i);

}

}

}.start();//消费者线程

newThread(){

@Overridepublic voidrun(){//消费10次

for(int j = 0; j < 5; j++){

System.out.println("Ready to Consume:" + (j + 1));

pc.consume(j);

}

}

}.start();

}

}classProduceConsumer{//生产者

public synchronized void produce(inti){if(isEmpty){//没东西了,可以生产

num = (int)(Math.random() * 100);

System.out.println("Producer:" + (i + 1) + "," +num);

isEmpty= false;

notify();

}else{try{

System.out.println("producer执行wait操作:" + (i + 1));

wait();

System.out.println("producer醒来:" + (i + 1));

num= (int)(Math.random() * 100);

System.out.println("Producer:" + (i + 1) + "," +num);

isEmpty= false;

notify();

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}//消费者

public synchronized void consume(inti){if(!isEmpty){

System.out.println("Consumer:" + (i + 1) + "," +num);

isEmpty= true;

notify();

}else{try{

System.out.println("consumer执行wait操作:" + (i + 1));

wait();

System.out.println("consumer醒来:" + (i + 1));

System.out.println("Consumer:" + (i + 1) + "," +num);

isEmpty= true;

notify();

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}publicProduceConsumer(){

isEmpty= true; //默认为空

}private boolean isEmpty; //是否为空

private int num; //生产的东西

public booleanisEmpty() {returnisEmpty;

}public void setEmpty(booleanisEmpty) {this.isEmpty =isEmpty;

}public intgetNum() {returnnum;

}public void setNum(intnum) {this.num =num;

}

}

线程同步属于线程的一个非常重要的知识,而且也相对比较复杂,这里只做一个简单的介绍,后面会有更加详细的讲解。

三、sleep()   其他类型阻塞

让当前的正在执行的线程暂停指定的时间,并进入阻塞状态。直接使用 Thread.sleep(long millionSeconds) 就可以了

packagecom.chanshuyi.thread;public classThreadDemo7 {public static voidmain(String[] args) {

Thread thread= newThread(){

@Overridepublic voidrun(){

System.out.println("Sleep 2 Seconds.");

}

};

thread.start();try{

Thread.sleep(2000);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("Awake");

}

}

四、yield()  线程让步

将线程从运行状态转换为就绪状态。

当某个线程调用 yiled() 方法从运行状态转换到就绪状态后,CPU 会从就绪状态线程队列中只会选择与该线程优先级相同或优先级更高的线程去执行。

在使用时直接用 Thread.yield() 静态方法就可以了。一般情况下我们的 CPU 都很难达到 100% 的利用率,所以当我们使用 yield() 方法将线程挂起之后,一般又会立即获得资源,继续执行,因此很难写个程序去进行验证。而且这个方法在工作中也是比较少用到,所以只需要了解其作用就可以了。

sleep() 和 yield() 两者的区别:

① sleep()方法会给其他线程运行的机会,不考虑其他线程的优先级,因此会给较低优先级线程一个运行的机会。yield()方法只会给相同优先级或者更高优先级的线程一个运行的机会。

② 当线程执行了 sleep(long millis) 方法,将转到阻塞状态,参数millis指定睡眠时间。当线程执行了yield()方法,将转到就绪状态。

③ sleep() 方法声明抛出InterruptedException异常,而 yield() 方法没有声明抛出任何异常。

packagecom.chanshuyi.thread;importjava.util.concurrent.locks.Lock;importjava.util.concurrent.locks.ReentrantLock;public classThreadDemo8 {public static voidmain(String[] args) {final Lock lock = newReentrantLock();

Thread thread1= newThread(){

@Overridepublic voidrun(){

System.out.println("Enter thread1.");

lock.lock();

System.out.println("Thread 1 had acquire the lock. Thread1 will sleep for 2 seconds");try{

Thread.sleep(2000);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

Thread.yield();

System.out.println("I'm the thread1");

lock.unlock();

}

};

Thread thread2= newThread(){

@Overridepublic voidrun(){

System.out.println("Enter thread2.");

lock.lock();

System.out.println("Thread 2 had acquire the lock. Thread2 will sleep for 2 seconds");try{

Thread.sleep(2000);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

Thread.yield();

System.out.println("I'm the thread2");

lock.unlock();

}

};

Thread thread3= newThread(){

@Overridepublic voidrun(){

System.out.println("Enter thread3.");

lock.lock();

System.out.println("Thread 3 had acquire the lock. Thread3 will sleep for 2 seconds");try{

Thread.sleep(2000);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

Thread.yield();

System.out.println("I'm the thread3");

lock.unlock();

}

};

thread1.setPriority(Thread.MIN_PRIORITY);

thread2.setPriority(Thread.NORM_PRIORITY);

thread3.setPriority(Thread.MAX_PRIORITY);

thread1.start();

thread2.start();

thread3.start();

}

}

在上面中,我们让3个线程竞争一个锁。其中一个线程随机获得lock锁,之后休眠两秒等待其他2个线程进入Lock Block状态。之后获得锁lock锁的线程调用yield()释放锁,这个时候,应该是优先级最高的那个线程获得锁,但实际上却不是这样的,具体原因我也没分析出来。下面是一些执行结果,感兴趣的朋友可以分析一下。

Enter thread2.

Thread 2 had acquire the lock. Thread2 will sleep for 2 seconds

Enter thread3.

Enter thread1.

I'm the thread2

Thread 3 had acquire the lock. Thread3 will sleep for 2 seconds

I'm the thread3

Thread 1 had acquire the lock. Thread1 will sleep for 2 seconds

I'm the thread1

Enter thread2.

Enter thread3.

Enter thread1.

Thread 2 had acquire the lock. Thread2 will sleep for 2 seconds

I'm the thread2

Thread 3 had acquire the lock. Thread3 will sleep for 2 seconds

I'm the thread3

Thread 1 had acquire the lock. Thread1 will sleep for 2 seconds

I'm the thread1

Enter thread3.

Thread 3 had acquire the lock. Thread3 will sleep for 2 seconds

Enter thread1.

Enter thread2.

I'm the thread3

Thread 1 had acquire the lock. Thread1 will sleep for 2 seconds

I'm the thread1

Thread 2 had acquire the lock. Thread2 will sleep for 2 seconds

I'm the thread2

Enter thread2.

Thread 2 had acquire the lock. Thread2 will sleep for 2 seconds

Enter thread3.

Enter thread1.

I'm the thread2

Thread 3 had acquire the lock. Thread3 will sleep for 2 seconds

I'm the thread3

Thread 1 had acquire the lock. Thread1 will sleep for 2 seconds

I'm the thread1

五、setPriority()  改变线程的优先级

每个线程在执行时都具有一定的优先级,优先级高的线程具有较多的执行机会。每个线程默认的优先级都与创建它的线程的优先级相同。main线程默认具有普通优先级。

设置线程优先级:setPriority(int priorityLevel)。参数priorityLevel范围在1-10之间,常用的有如下三个静态常量值:

MAX_PRIORITY:10

MIN_PRIORITY:1

NORM_PRIORITY:5

获取线程优先级:getPriority()。

注:具有较高线程优先级的线程对象仅表示此线程具有较多的执行机会,而非优先执行。

packagecom.chanshuyi.thread;public classThreadDemo7 {public static voidmain(String[] args) {

Thread thread= newThread(){

@Overridepublic voidrun(){

System.out.println("I'm the Priority Test Thread.");

}

};

thread.setPriority(Thread.MAX_PRIORITY);

thread.start();

}

}

六、setDaemon(true)  设置为后台线程

概念/目的:后台线程主要是为其他线程(相对可以称之为前台线程)提供服务,或“守护线程”。如JVM中的垃圾回收线程。

生命周期:后台线程的生命周期与前台线程生命周期有一定关联。主要体现在:当所有的前台线程都进入死亡状态时,后台线程会自动死亡(其实这个也很好理解,因为后台线程存在的目的在于为前台线程服务的,既然所有的前台线程都死亡了,那它自己还留着有什么用...伟大啊 ! !)。

设置后台线程:调用Thread对象的setDaemon(true)方法可以将指定的线程设置为后台线程。

packagecom.chanshuyi.thread;public classThreadDemo91 {public static voidmain(String[] args) {//输出:Main Thread is going to die

Thread thread = newThread(){

@Overridepublic voidrun(){try{

Thread.sleep(1000);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("Hello, I'm background thread.");

}

};

thread.setDaemon(true);

thread.start();

System.out.println("Main Thread is going to die.");

}

}

上面的代码中存在两个线程,一个是Main线程,是前台线程,一个是我们创建的后台线程。我们在后台线程中故意使其休眠了1秒,而在这1秒钟内前台线程Main已经执行完毕了,所以后台线程也就直接结束了”Main Thread is going to die.“,而不会输出后台线程中的语句。

判断线程是否是后台线程:调用thread对象的isDeamon()方法。

注:main线程默认是前台线程,前台线程创建中创建的子线程默认是前台线程,后台线程中创建的线程默认是后台线程。调用setDeamon(true)方法将前台线程设置为后台线程时,需要在start()方法调用之前,否则一但线程运行,将无法改变其类型。前天线程都死亡后,JVM通知后台线程死亡,但从接收指令到作出响应,需要一定的时间。

1326fe9baef02cf510ab9bc2b60252bd.png

参考博文:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值