并发

作者: tdeblog | 来源:发表于2016-08-15 17:04 被阅读0次

    1.Runable与Thread#

    Thread类是实现了Runable接口。

    class TestRun implements Runnable {
    
        private int i = 0;
        @Override
        public void run() {
            while(i < 100) {
                i ++;
                System.out.println("run");
                Thread.yield();
            }
        }
    }
    
    public class Client {
        public static void main(String args[]) {
            /*Context context = new Context();
            context.setState(Context.concreteState1);
            context.handler2();
            context.handler1();*/
            TestRun tr = new TestRun();
            tr.run();
            Thread t = new Thread(new TestRun());
            //设置为后台执行
            t.setDaemon(true);
            //与主线程并发执行
            t.start();
            
            System.out.println("main");
        }
    }
    

    2.Executors#

    class TestRun implements Runnable {
    
        private int i = 0;
        private int n = 0;
    
        public TestRun(int n){
            this.n = n;
        }
        @Override
        public void run() {
            while(i < 100) {
                i ++;
                System.out.println("run" + n);
                Thread.yield();
            }
        }
    }
    
    public class Client {
        public static void main(String args[]) {
            //一个线程管理工具类
            //ExecutorService ex = Executors.newCachedThreadPool();
            //定义线程数量
            ExecutorService ex = Executors.newFixedThreadPool(3);
            for(int i = 0; i <= 5; i++) {
                ex.execute(new TestRun(i));
            }
            ex.shutdown();
        }
    }
    

    3.Callable#

    Callable接口可以处理具有返回值的线程。只能通过ExecutorService.submit()函数启动

    class TestCall implements Callable<String> {
    
        private StringBuilder sb = new StringBuilder("Callable");
        private int id;
    
        public TestCall(int id) {
            this.id = id;
        }
    
        @Override
        public String call() throws Exception {
            sb.append(id);
            return sb.toString();
        }
    }
    
    public class Client {
        public static void main(String args[]) {
            ExecutorService ex = Executors.newCachedThreadPool();
            ArrayList<Future<String>> results = new ArrayList<Future<String>>();
            for(int i = 0; i <= 1000; i++) {
                results.add(ex.submit(new TestCall(i)));
            }
    
            for(Future<String> fs : results) {
                try {
                    System.out.println(fs.get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                } finally {
                    ex.shutdown();
                }
            }
        }
    }
    

    4.共享资源#

    //一个加锁对象中某个加锁方法被访问,则整个对象被加锁
    synchronized void f() {}
    synchronized void g() {}
    //整个类被加锁
    synchronized static void t() {}
    
    //显示加锁
    lock.lock()
    lock.unlock()
    ``

    相关文章

      网友评论

          本文标题:并发

          本文链接:https://www.haomeiwen.com/subject/gqvusttx.html