线程优先级及volatile关键字

一、线程优先级

/**
 * @author Zhou
 * @Date:creat Time 2017/8/23
 * java 中可以设置线程的优先级 优先级高的线程在竞争资源时会更具优势 更可能得到cpu执行资源
 * 易产生线程饥饿状态 低优先级线程一直未能抢占到cpu资源
 *
 *
 */
public class PriorityDemo {
    public  static class  HightProity extends Thread{
        static int count=0;
        @Override
        public void run(){
            while (true) {
                synchronized (PriorityDemo.class){
                    count++;
                    if(count>100000) {
                        System.out.println("高优先级执行结束!");
                        break;
                    }
                }
            }
        }
    }
    public  static class  LowerProity extends Thread{
        static int count=0;
        @Override
        public void run(){
            while (true) {
                synchronized (PriorityDemo.class){
                    count++;
                    if(count>100000) {
                        System.out.println("低优先级执行结束!");
                        break;
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        /**
         * 绝大多数情况下 高优先级先执行完 但不保证都是这样
         */
        Thread high=new HightProity();
        LowerProity low=new LowerProity();
        high.setPriority(Thread.MAX_PRIORITY);
        low.setPriority(Thread.MIN_PRIORITY);
        low.start();
        high.start();
    }
}

二、volatile关键字

/**
 * @author Zhou
 * @Date:creat Time 2017/8/23
 * volatile 易变的/不稳定的
 * 它可以保证数据的可见性 有序性
 * 不保证原子性!!
 */
public class VolatileDemo {
    /**
     * 例子:原子性
     */
    static volatile int i=0;
    public static class PlusTask implements Runnable{

        @Override
        public void run() {
            for (int j = 0; j < 10000; j++) {
                i++;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread[] threads=new Thread[10];
        for (int j = 0; j < 10; j++) {
            threads[j]=new Thread(new PlusTask());
            threads[j].start();
        }
        for (int j = 0; j < 10; j++) {
            threads[j].join();//阻塞main主线程 无限等待 当threads数组所有线程执行完后 主线程在执行
        }
        /**
         * 若无多线程问题 执行结果应为100000 但实际输出小于100000
         * volatile不保证原子性!
         */
        System.out.println("多线程下i="+i);//
    }

    /**
     * 例子:可见性
     */
    public static class Novisibility{
        /**
         *
         */
        private volatile  static boolean ready;
        private static int number;
        private static class ReadThread extends Thread{
            @Override
            public void run(){
                while (!ready);
                System.out.println(number);
            }
        }

        public static void main(String[] args) throws InterruptedException {
            /**
             * 若 ready不是volatile readThread线程并不能发现主线程对read的修改 将陷入死循环 执行结果如下
             主线程睡眠
             主线程睡眠结束
             主线程第二次睡眠
             主线程第二次睡眠end
             *
             * 若read为volatile 将为其保证可见性 任何对volatile修饰的变量写操作将及时从工作内存刷新到主内存中 保证可见性 执行结果:
             *
             主线程睡眠
             主线程睡眠结束
             主线程第二次睡眠
             111
             主线程第二次睡眠end
             */
            new ReadThread().start();
            System.out.println("主线程睡眠");
            Thread.sleep(1000);
            System.out.println("主线程睡眠结束");
            number=111;
            ready=true;
            System.out.println("主线程第二次睡眠");
            Thread.sleep(1000);
            System.out.println("主线程第二次睡眠end");

        }
    }


}

转载于:https://my.oschina.net/NeverMoreMoon/blog/1522688

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值