码迷,mamicode.com
首页 > 编程语言 > 详细

线程开发中的资源操作

时间:2014-09-23 23:51:35      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   strong   div   sp   on   

理论:

  临界资源

    PV操作:通过信号量机制进行维护关系资源的安全---看看操作系统....

  任务对资源进行操作,为了安全要加锁,锁加载临界资源操作上(也就是对共享资源的操作)

    加锁 

      共享资源操作

    解锁

编程模型:

   1.通过锁

   2.threadLocal

   3.final 

例子:

public static void main(String[] args) {
        
        JobLanuch jobLanuch = new JobLanuch();
        
        
        Task_A task_a = new Task_A(jobLanuch.threadLocal);
        Task_B task_b =  new Task_B(jobLanuch.threadLocal);
        
        jobLanuch.submit(task_a);
        jobLanuch.submit(task_b);
        
    }

    public static class JobLanuch {
        private ExecutorService executor = Executors.newFixedThreadPool(10);
        
        //共享资源
        public ThreadLocal<String> threadLocal = new ThreadLocal<String>() {
            
            //初始化资源
            @Override
            protected String initialValue() {
                return "initThreadLocal";
            }

        };

        void submit(Runnable runnableTask) {
            executor.submit(runnableTask);
        }

    }

    public static class Task_A implements Runnable {
        
        //共享资源接入点
        private ThreadLocal<String> threadLocal;

        public Task_A(ThreadLocal<String> threadLocal) {
            this.threadLocal = threadLocal;
        }

        @Override
        public void run() {
            threadLocal.set("Task_A-50");
            
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //具体的业务挂接点.....
            System.out.println("Task_A do...." + "\n" + threadLocal.get());
            
        }

    }

    public static class Task_B implements Runnable {

        //共享资源接入点
        private ThreadLocal<String> threadLocal;

        public Task_B(ThreadLocal<String> threadLocal) {
            this.threadLocal = threadLocal;
        }

        @Override
        public void run() {
            threadLocal.set("Task_B-50");
            //具体的业务挂接点.....
            System.out.println("Task_B do...."+ "\n" + threadLocal.get());
        }

    }

 

public static void main(String[] args) {

        //job管理器
        JobLanuch jobLanuch = new JobLanuch();
        
        //任务
        Task_A task_a = new Task_A(jobLanuch.lists);//挂接上共享资源
        Task_B task_b = new Task_B(jobLanuch.lists);//挂接上共享资源
        
        
        //提交任务
        jobLanuch.submit(task_a);
        jobLanuch.submit(task_b);
    }

    public static class JobLanuch {
        // 线程池
        private ExecutorService executor = Executors.newFixedThreadPool(10);

        // 共享资源
        public volatile List<String> lists = new ArrayList<String>();

        public JobLanuch() {

            // 模拟初始化资源
            lists.add("apple");
            lists.add("balanca");

        }

        // 提交任务
        public void submit(Runnable runnableTask) {
            executor.submit(runnableTask);
        }

    }

    public static class Task_A implements Runnable {
        //共享资源
        public volatile List<String> lists;

        public Task_A(List<String> lists) {
            this.lists = lists;
        }

        @Override
        public void run() {

            try {
                //模拟业务操作...可能是低效的网络IO...
                lists.add(0, "Task_A put new Apple doing....");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 具体的业务挂接点.....
            System.out.println("Task_A do....:" + lists.get(0) );

        }

    }

    public static class Task_B implements Runnable {
        //共享资源
        public volatile List<String> lists;

        public Task_B(List<String> lists) {
            this.lists = lists;
        }

        @Override
        public void run() {
            // 具体的业务挂接点.....
            System.out.println("Task_B do....:" + lists.get(0) );
            lists.add(0,"Task_B put a apple finish !!!");
        }

    }

 

线程开发中的资源操作

标签:style   blog   color   io   ar   strong   div   sp   on   

原文地址:http://www.cnblogs.com/gstsyyb/p/3989388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!