并发

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()
``
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 译序 本指南根据 Jakob Jenkov 最新博客翻译,请随时关注博客更新:http://tutorials.j...
    高广超阅读 5,262评论 1 68
  • 一.线程安全性 线程安全是建立在对于对象状态访问操作进行管理,特别是对共享的与可变的状态的访问 解释下上面的话: ...
    黄大大吃不胖阅读 893评论 0 3
  • 一、并发 进程:每个进程都拥有自己的一套变量 线程:线程之间共享数据 1.线程 Java中为多线程任务提供了很多的...
    SeanMa阅读 2,599评论 0 11
  • 原文链接 译者:靖靖 并发 进程和线程 在并发编程当中,有两个基本的执行单元:进程和线程。在java中,我们大部分...
    4b4f3ceb6f71阅读 822评论 4 16
  • layout: posttitle: 《Java并发编程的艺术》笔记categories: Javaexcerpt...
    xiaogmail阅读 5,923评论 1 19