java程序员-http://www.it-ebooks.info/

年轻的我,脚踩大地,仰望天空,路还很长....

博客园 首页 新随笔 联系 订阅 管理

理论:

  临界资源

    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 !!!");
        }

    }

 

//读写分离---写加锁而读没有加锁,使用copy 内存可见性
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 CopyOnWriteArrayList<String> lists = new CopyOnWriteArrayList<String>(); public JobLanuch() { } // 提交任务 public void submit(Runnable runnableTask) { executor.submit(runnableTask); } } public static class Task_A implements Runnable { //共享资源 public CopyOnWriteArrayList<String> lists; public Task_A(CopyOnWriteArrayList<String> lists) { this.lists = lists; } @Override public void run() { try { //模拟业务操作,读操作.... lists.add("Task_A");//写入 Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static class Task_B implements Runnable { //共享资源 public final List<String> lists; public Task_B(List<String> lists) { this.lists = lists; } @Override public void run() { // 具体的业务挂接点..... System.out.println(lists.toString());//读操作... } }

 

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 List<String> lists = new ArrayList<String>();
        
        public JobLanuch() {
        }

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

    }

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

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

        @Override
        public void run() {

            try {
                synchronized (lists) {//对共享资源加锁
                    //模拟业务操作,读操作....
                    lists.add("Task_A");//写入
                }
                
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
        }

    }

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

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

        @Override
        public void run() {
            // 具体的业务挂接点.....
            System.out.println(lists.toString());//读操作...
        }

    }

 线程之间协作

wait,notify---典型编程模型:

  生产者消费者模式

  lock+wait、notify

 

posted on 2014-09-23 21:37  gstsyyb  阅读(194)  评论(0编辑  收藏  举报