java并发编程(二)-休眠、优先级

6 篇文章 0 订阅
6 篇文章 0 订阅

续接java并发编程(一),本篇文章主要分析线程休眠、优先级、让步,例子沿用对《java编程思想》练习题进行改编。

  • 休眠
练习一:创建一个任务,它休眠1至10秒之间的随机数量的时间,然后显示它的休眠时间并退出。创建并运行一定数量的这种任务
package twentyoneThread;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class ExcFive implements Runnable {
    private final int maxSleepTime = 10;
    private int sleepTime;
    private static int taskCount = 0;
    private final int id = taskCount++;
    private Random random=new Random();
   //返回休眠时间
    public String Status() {
        return "#" + id + " thread sleep " + sleepTime + "secends";

    }

    public ExcFive() {
    sleepTime = random.nextInt(maxSleepTime)+1;//生成随机休眠数

    }

    @Override
    public void run() {
        try {
            TimeUnit.MILLISECONDS.sleep(sleepTime);//推荐休眠时间的写法
            System.out.println(Status());
            Thread.yield();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
}

public class TestExcFive {
    public static void main(String[] args) {
        ExecutorService exes = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            exes.execute(new ExcFive());

        }
        exes.shutdown();
    }
}

输出式样:

#4 thread sleep 2secends
#2 thread sleep 7secends
#3 thread sleep 7secends
#1 thread sleep 10secends
#0 thread sleep 9secends
  • 优先级

线程优先级将线程的重要性传递给了调度器。然而,这并不是意味着优先权较低的线程将得不到执行(也就是说,优先权不会导致死锁)。较低的优先级的线程仅仅是执行的频率较低。

package twentyoneThread;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class ExcSix implements Runnable {
    private int countDown = 3;
    private int priority;
    private static int taskCount = 0;
    private final int id = taskCount++;

    public String Status() {
        return  Thread.currentThread()+"#" + id +"priortity=" +Thread.currentThread().getPriority()+"countDown="+countDown;

    }

    public ExcSix(int priortity) {

       this.priority= priortity;
    }

    @Override
    public void run() {
        Thread.currentThread().setPriority(priority);
        while (countDown-- > 0) {
            System.out.println(Status());
            Thread.yield();
        }

    }

}

public class TestExcSix {
    public static void main(String[] args) {
        ExecutorService exes = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++) {
    exes.execute(new ExcSix(Thread.MIN_PRIORITY));
    exes.execute(new ExcSix(Thread.MAX_PRIORITY));

}


        exes.shutdown();
    }
}

输出式样:

Thread[pool-1-thread-2,10,main]#1priortity=10countDown=2
Thread[pool-1-thread-4,10,main]#3priortity=10countDown=2
Thread[pool-1-thread-2,10,main]#1priortity=10countDown=1
Thread[pool-1-thread-4,10,main]#3priortity=10countDown=1
Thread[pool-1-thread-2,10,main]#1priortity=10countDown=0
Thread[pool-1-thread-4,10,main]#3priortity=10countDown=0
Thread[pool-1-thread-3,1,main]#2priortity=1countDown=2
Thread[pool-1-thread-5,1,main]#4priortity=1countDown=2
Thread[pool-1-thread-7,1,main]#6priortity=1countDown=2
Thread[pool-1-thread-9,1,main]#8priortity=1countDown=2
Thread[pool-1-thread-1,1,main]#0priortity=1countDown=2
Thread[pool-1-thread-3,1,main]#2priortity=1countDown=1
Thread[pool-1-thread-6,10,main]#5priortity=10countDown=2
Thread[pool-1-thread-5,1,main]#4priortity=1countDown=1
Thread[pool-1-thread-7,1,main]#6priortity=1countDown=1
Thread[pool-1-thread-9,1,main]#8priortity=1countDown=1
Thread[pool-1-thread-8,10,main]#7priortity=10countDown=2
Thread[pool-1-thread-6,10,main]#5priortity=10countDown=1
Thread[pool-1-thread-8,10,main]#7priortity=10countDown=1
Thread[pool-1-thread-3,1,main]#2priortity=1countDown=0
Thread[pool-1-thread-1,1,main]#0priortity=1countDown=1
Thread[pool-1-thread-9,1,main]#8priortity=1countDown=0
Thread[pool-1-thread-7,1,main]#6priortity=1countDown=0
Thread[pool-1-thread-5,1,main]#4priortity=1countDown=0
Thread[pool-1-thread-6,10,main]#5priortity=10countDown=0
Thread[pool-1-thread-8,10,main]#7priortity=10countDown=0
Thread[pool-1-thread-1,1,main]#0priortity=1countDown=0
Thread[pool-1-thread-10,10,main]#9priortity=10countDown=2
Thread[pool-1-thread-10,10,main]#9priortity=10countDown=1
Thread[pool-1-thread-10,10,main]#9priortity=10countDown=0
  • 总结

《java编程思想》里面关于线程的讲解很详细,篇幅较长,涉及的知识点也多,把里面的练习题都梳理一篇花费的时间也蛮久,太急切于梳理线程很可能会觉得枯燥、无味,坚持不下去。所以以后有条件,每天写一篇关于线程的理解,同时工作、学习中遇到的其它的问题都会试着去用博客的形式记录下来。对于写博客我自己还是一个新手,不是很适应,往往一篇很简短的文章自己都要写好久,也不会表达自己的感想。希望从今天开始自己以后每天坚持写博客,记录学习的点滴。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值