高并发编程二

  1. 分门类的管理:线程组
public class ThreadGroupTest implements Runnable{

    @Override
    public void run() {
        String groupName = Thread.currentThread().getThreadGroup().getName()
                 + "-" + Thread.currentThread().getName();

        while (true) {
            System.out.println("this is " + groupName );

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        ThreadGroup threadGroup = new ThreadGroup("testGroup");
        Thread t1 = new Thread(threadGroup,new ThreadGroupTest(),"T1");
        Thread t2 = new Thread(threadGroup,new ThreadGroupTest(),"T2");

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

        System.out.println("线程组执行数:" + threadGroup.activeCount());
        threadGroup.list();
    }
}

执行结果:
线程组执行数:2

 java.lang.ThreadGroup[name=testGroup,maxpri=10]
        Thread[T1,5,testGroup]
        Thread[T2,5,testGroup]
    this is testGroup-T1
    this is testGroup-T2
    this is testGroup-T2
    this is testGroup-T1
    this is testGroup-T2
    this is testGroup-T1
    ...
线程组有一个值得注意的方法 stop(), 会停止线程组所有线程。
但是它会遇到和 Thread.stop() 相同的问题。
  1. 驻守后台:守护线程(Daemon) - 垃圾回收线程、JIT线程
    当一个 Java应用内,只有守护线程时,Java虚拟机就会自然退出。
    public class DaemoDemo {
        public static class DaemoT extends Thread {
            @Override
            public void run() {
                while (true) {
                    System.out.println("this is daemon thread");

                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        public static void main(String[] args) throws InterruptedException {
            DaemoT t = new DaemoT();
            t.setDaemon(true);
            t.start();

            Thread.sleep(100);
        }
    }
只需将 setDaemon() 设为true,必须在 start() 调用之前
  1. 线程优先级
    在Java中,使用 1 到 10 表示线程优先级。内置三个静态标量
    public final static int MIN_PRIORITY = 1;
    public final static int NORM_PRIORITY = 5;
    public final static int MAX_PRIORITY = 10;
数字越大优先级越高。
    public class PriorityDemo {
        public static class HightPriority extends Thread {
            static int count = 0;

            @Override
            public void run() {
                while (true) {
                    synchronized (PriorityDemo.class) {
                        count++;
                        if(count > 100000) {
                            System.out.println("HightPriority is complete");
                            break;
                        }
                    }
                }

            }
        }

        public static class LowPriority extends Thread {
            static int count = 0;

            @Override
            public void run() {
                while (true) {
                    synchronized (PriorityDemo.class) {
                        count++;
                        if (count > 100000) {
                            System.out.println("LowPriority complete");
                            break;
                        }
                    }
                }
            }
        }

        public static void main(String[] args) {
            Thread hight = new HightPriority();
            Thread low = new LowPriority();

            hight.setPriority(Thread.MAX_PRIORITY);
            low.setPriority(Thread.MIN_PRIORITY);

            low.start();
            hight.start();
        }
    }
执行结果:
HightPriority is complete
LowPriority complete
  1. 线程安全的概念与 synchronized
    public class AccountVol implements Runnable {
        static AccountVol instance = new AccountVol();
        static volatile int i = 0;
        public static synchronized void increase() {
            i++;
        }
        @Override
        public void run() {
            for (int j = 0;j < 100000;j++) {
                /*synchronized (instance) {
                    increase();
                }*/
                increase();

            }
        }

        public static void main(String[] args) throws InterruptedException {
           /* Thread t1 = new Thread(instance);
            Thread t2 = new Thread(instance);*/
            Thread t1 = new Thread(new AccountVol());
            Thread t2 = new Thread(new AccountVol());

            t1.start();t2.start();
            t1.join();t2.join();

            System.out.println(i);
        }
    }
  1. 并发下的 ArrayList
    public class ThreadForArrayList {
        static List<Integer> al = new ArrayList<Integer>();

        public static class AddThread implements Runnable {

            @Override
            public void run() {
                for (int i = 0;i < 100000;i++) {
                    al.add(i);
                }
            }
        }

        public static void main(String[] args) throws InterruptedException {
            Thread t1 = new Thread(new AddThread());
            Thread t2 = new Thread(new AddThread());
            t1.start();t2.start();
            t1.join();t2.join();

            System.out.println(al.size());

        }
    }
执行结果:
1.
Exception in thread "Thread-1" java.lang.ArrayIndexOutOfBoundsException: 10
at java.util.ArrayList.add(ArrayList.java:459)
at com.xc.blackCap.thread.ThreadForArrayList$AddTrade.run(ThreadForArrayList.java:17)
at java.lang.Thread.run(Thread.java:745)
100005
2. 数组 size 小于 200000

改进方法:Vector 代替 ArrayList
  1. 并发下的 HashMap
    public class ThreadForHashMap {
        //static Map<String,String> map = new HashMap<String,String>();
        static Map<String,String> map = new ConcurrentHashMap<String,String>();

        public static class AddThread implements Runnable {
            int start = 0;
            public AddThread(int start) {
                this.start = start;
            }
            @Override
            public void run() {
                for (int i = 0;i < 100000;i+=2) {
                    map.put(String.valueOf(i),Integer.toBinaryString(i));
                }
            }
        }

        public static void main(String[] args) throws InterruptedException {
            Thread t1 = new Thread(new AddThread(0));
            Thread t2 = new Thread(new AddThread(1));

            t1.start();t2.start();
            t1.join();t2.join();

            System.out.println(map.size());
        }

    }
调整:使用 ConcurrentHashMap 代替 HashMap
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值