美文网首页java进阶干货Java学习笔记Java 杂谈
Java并发编程 -- 线程安全、优先级设定

Java并发编程 -- 线程安全、优先级设定

作者: FantJ | 来源:发表于2018-02-25 11:04 被阅读33次

优先级设定

一个合理的优先级可以在一定条件下避免一些活跃性问题,比如死锁、饥饿等

public class Task implements Runnable{

    @Override
    public void run() {
        while (true) {
            System.out.println(Thread.currentThread().getName() + "线程执行了...");
        }
    }
}
public class PriorityTest {
    public static void main(String[] args) {
        Thread t1 = new Thread(new Task());
        Thread t2 = new Thread(new Task());

        /**
         * 设置优先级
         * MAX_PRIORITY=10
         * MIN_PRIORITY=1
         * NORM_PRIORITY=5
         */

        t1.setPriority(Thread.NORM_PRIORITY);
        t2.setPriority(Thread.MAX_PRIORITY);

        t1.start();
        t2.start();
    }
}

线程安全问题解决

当多个线程在非原子处理下操作相同的资源时,难免出现资源的混乱。

我在这里举个value++的例子。

无线程安全时
public class Task{

        public int value = 0;


       // 没有处理线程安全
        public  int getValue() {
        return value++;
    }
    public static void main(String[] args) {

        Task task = new Task();
        new Thread(){
            @Override
            public void run() {
                while (true) {
                    System.out.println(Thread.currentThread().getName() + "  " + task.getValue());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                while (true) {
                    System.out.println(Thread.currentThread().getName() + "  " + task.getValue());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
}
image.png

我们会发现2和5有重复。这是jvm底层的问题,我在后文有分析。

处理线程安全

如果我们给线程加锁,将其变成原子处理,就会解决该问题。

    public  synchronized int getValue() {
        return value++;
    }

既在修饰符后加上synchronized关键字。


image.png

但是又有一个问题,这样的话,其实原理上是串行处理的,那我们该如果解决这个问题呢。

相关文章

网友评论

    本文标题:Java并发编程 -- 线程安全、优先级设定

    本文链接:https://www.haomeiwen.com/subject/fimlxftx.html