zoukankan      html  css  js  c++  java
  • 多线程及线程并发库Executors

    1.join() 优先执行这个线程

    2.yield() 让出本次执行时间片

    3.setDaemon(true) 线程可以变成守护线程

    import java.util.concurrent.locks.ReentrantLock;
    
    public class thread {
        public static void main(String[] args){
            MyThread myThread = new MyThread();
            Thread thread = new Thread(myThread);
            Thread thread1 = new Thread(myThread);
            thread.start();
            thread1.start();
        }
    }
    class MyThread implements Runnable{
    
        private int ticket = 10;
        @Override
        public void run() {
            for (int i=0;i<100;i++){
               method();
                }
            }
        ReentrantLock lock = new ReentrantLock();
            private void method() {
                //
                lock.lock();
                try {
                    if(ticket>0){
                        ticket--;
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(ticket);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    //解锁
                    lock.unlock();
                }
        }
    
    }

     -------------------------------------------------------------------

    线程池

    线程池的作用:

    1.降低资源消耗

    重复利用已创建的线程降低线程创建和销毁造成的消耗

    2.提高响应速度

    当任务到达时,不需要创建线程就可以立刻执行

    3.提高线程的可管理性

    对线程池可以进行统一分配,调优

    Executors类

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    
    /*
    *  线程池
    * */
    public class Demo {
    
        public static void main(String[] args) {
    //        创建单线程
    //        ExecutorService es = Executors.newSingleThreadExecutor();
    //        创建固定线程
    //        ExecutorService es = Executors.newFixedThreadPool(2);
    //        创建缓存线程无大小
    //        ExecutorService es = Executors.newCachedThreadPool();
            ScheduledExecutorService es = Executors.newScheduledThreadPool(3);
    //        es.execute(new demo1());
    //        es.execute(new demo1());
    //        调度,延迟3秒执行
            es.schedule(new demo1(),3000, TimeUnit.MILLISECONDS);
            es.shutdown();
        }
    }
    class demo1 implements Runnable{
    
        @Override
        public void run() {
            for (int i=0;i<10;i++){
                System.out.println(Thread.currentThread().getName()+"---"+i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 相关阅读:
    聊聊WS-Federation
    用双十一的故事串起碎片的网络协议(上)
    责任链模式的使用-Netty ChannelPipeline和Mina IoFilterChain分析
    最小化局部边际的合并聚类算法(中篇)
    最小化局部边际的合并聚类算法(上篇)
    UVaLive 7371 Triangle (水题,判矩形)
    UVaLive 7372 Excellence (水题,贪心)
    POJ 3312 Mahershalalhashbaz, Nebuchadnezzar, and Billy Bob Benjamin Go to the Regionals (水题,贪心)
    UVa 1252 Twenty Questions (状压DP+记忆化搜索)
    UVa 10817 Headmaster's Headache (状压DP+记忆化搜索)
  • 原文地址:https://www.cnblogs.com/mm163/p/10674506.html
Copyright © 2011-2022 走看看