zoukankan      html  css  js  c++  java
  • Java线程池

    缺少更多功能,如更多执行,定期执行,线程中断

     线程池中的状态:

     

     

    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    
    @Slf4j
    public class ThreadPoolExample {
        public static void main(String[] args) {
           // ExecutorService executorService= Executors.newCachedThreadPool();
           // ExecutorService executorService=Executors.newSingleThreadExecutor();
            //ExecutorService executorService=Executors.newFixedThreadPool(3);
            ExecutorService executorService=Executors.newScheduledThreadPool(3);
            executorService.schedule(new Runnable(){
                @Override
                public void run(){
                     log.warn("schedule run");
                }
            },3, TimeUnit.SECONDS);
            //延迟一秒每隔一秒执行
            executorService.scheduleAtFixedRate(new Runnable(){
                @Override
                public void run(){
                    log.warn("schedule run");
                }
            },1,3,TimeUnit.SECONDS);
    
            Timer timer=new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    log.warn("schedule run");
                }
            },new Date(),5*1000);
    
    //        for(int i=0;i<10;i++){
    //            final  int index=i;
    //            executorService.execute(new Runnable() {
    //                @Override
    //                public void run() {
    //                    log.info("task:{}",index);
    //                }
    //            });
    //        }
            executorService.shutdown();
        }
    }
    

      

     

    死锁案例:

    import lombok.extern.slf4j.Slf4j;
    
    @Slf4j
    public class DeadLock implements Runnable {
        public int flag=1;
        private static Object o1=new Object(),o2=new Object();
        @Override
        public void run(){
            log.info("flag:",flag);
            if(flag==1){
                synchronized (o1){
                    try {
                        Thread.sleep(500);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    synchronized (o2){
                        log.info("1");
                    }
                }
            }
            if(flag==0){
                synchronized (o2){
                    try {
                        Thread.sleep(500);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    synchronized (o1){
                       log.info("0");
                    }
                }
            }
    
        }
    
        public static void main(String[] args) {
            DeadLock td1=new DeadLock();
            DeadLock td2=new DeadLock();
            td1.flag=1;
            td2.flag=0;
            new Thread(td1).start();
            new Thread(td2).start();
        }
    }

    多线程并发的实践:

    1.使用本地变量

    2.使用不可变类

    3.最小化锁的作用域范围

    4.使用线程池的Executor,而不是直接new Thread执行

    5.宁可使用同步,也不使用线程的wait和notify

    6.使用BlockingQueue实现生产消费模式

    7.使用并发集合不是加了锁的同步集合

    8.使用Semaphore创建有界的访问

    9.宁可使用同步代码块,也不使用同步的方法

    10避免使用静态变量

  • 相关阅读:
    压缩打包linux 文件压缩解压命令Strut2教程java教程
    【leetcode】Valid Parentheses
    指向数据个人对指针的理解,欢迎大家批评Strut2教程java教程
    UML中的用例图
    Extjs4中组件的查找方式
    Extjs中选择组件、选择元素的方法
    [转载]C 内存管理详解
    C开发机顶盒实战代码之队列
    C机顶盒开发实战常用初始化类型:数组、结构、指针
    [转载]C语言链表的建立、插入和删除
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/11317573.html
Copyright © 2011-2022 走看看