JAVA线程-示例代码

来源:互联网 发布:淘宝app我的分享在哪里 编辑:程序博客网 时间:2024/05/12 20:07

线程同步

public class Test {    public static void main(String[] args) {        class Person {            public String name;            private String gender;            public void set(String name, String gender) {                this.name = name;                this.gender = gender;            }            public void get() {                System.out.println(this.name + "...." + this.gender);            }        }        final Person p = new Person();        new Thread(new Runnable() {            public void run() {                int x = 0;                while (true) {                    synchronized (p) {                        if (x == 0) {                            p.set("张三", "男");                        } else {                            p.set("lili", "nv");                        }                        x = (x + 1) % 2;                        try {                            Thread.sleep(5);                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                    }                }            }        }).start();        new Thread(new Runnable() {            public void run() {                while (true) {                    synchronized (p) {                        p.get();                    }                }            }        }).start();    }}

线程间通讯

/* *线程等待唤醒机制 *等待和唤醒必须是同一把锁 */public class Test {    private static boolean flags =false;    public static void main(String[] args){        class Person{            public String name;            private String gender;            public void set(String name,String gender){                this.name =name;                this.gender =gender;            }            public void get(){                System.out.println(this.name+"...."+this.gender);            }        }        final Person p =new Person();        new Thread(new Runnable(){            public void run(){                int x=0;                while(true){                    synchronized (p) {                        if(flags)                            try {                                p.wait();                            } catch (InterruptedException e) {                                // TODO Auto-generated catch block                                e.printStackTrace();                            };                        if(x==0){                            p.set("张三", "男");                        }else{                            p.set("lili", "nv");                        }                        x=(x+1)%2;                        flags =true;                        p.notifyAll();                    }                }            }        }).start();        new Thread(new Runnable(){            public void run(){                while(true){                    synchronized (p) {                        if(!flags)                            try {                                p.wait();                            } catch (InterruptedException e) {                                // TODO Auto-generated catch block                                e.printStackTrace();                            };                        p.get();                        flags =false;                        p.notifyAll();                    }                }            }        }).start();    }}

生产者与消费者

/** * 仓库类Storage实现缓冲区 */class Storage {    boolean test = true;    // 仓库最大存储量    private final int MAX_SIZE = 100;    // 仓库存储的载体    private LinkedList<Object> list = new LinkedList<Object>();    // 生产num个产品    public void produce(int num) {        // 同步代码段,生产时,只能一个生产者        synchronized (list) {            // 如果仓库剩余容量不足,等待            while (list.size() + num > MAX_SIZE) {                System.out.println("【要生产的产品数量】:" + num + "/t【库存量】:"                        + list.size() + "/t暂时不能执行生产任务!");                try {                    // 由于条件不满足,生产阻塞(等待消费后的唤醒)                    list.wait();                } catch (InterruptedException e) {                    e.printStackTrace();                }            }            // 生产条件满足情况下,生产num个产品            for (int i = 1; i <= num; ++i) {                list.add(new Object());            }            System.out.println("【已经生产产品数】:" + num + "/t【现仓储量为】:" + list.size());            //唤醒(唤醒仓库存储量不足时的等待)            list.notifyAll();        }    }    // 消费num个产品    public void consume(int num) {        // 同步代码段        synchronized (list) {            // 如果仓库存储量不足            while (list.size() < num) {                System.out.println("【要消费的产品数量】:" + num + "/t【库存量】:"                        + list.size() + "/t暂时不能执行消费任务!");                try {                    // 由于条件不满足,消费阻塞(等待生产后的唤醒)                    list.wait();                } catch (InterruptedException e) {                    e.printStackTrace();                }            }            // 消费条件满足情况下,消费num个产品            for (int i = 1; i <= num; ++i) {                list.remove();            }            System.out.println("【已经消费产品数】:" + num + "/t【现仓储量为】:" + list.size());//唤醒存储量不足时的等待            list.notifyAll();        }    }    // get/set方法    public LinkedList<Object> getList() {        return list;    }    public void setList(LinkedList<Object> list) {        this.list = list;    }    public int getMAX_SIZE() {        return MAX_SIZE;    }}/** * 生产者类Producer继承线程类Thread */class Producer extends Thread {    // 每次生产的产品数量    private int num;    // 所在放置的仓库    private Storage storage;    // 构造函数,设置仓库    public Producer(Storage storage) {        this.storage = storage;    }    // 线程run函数    public void run() {        produce(num);    }    // 调用仓库Storage的生产函数    public void produce(int num) {        storage.produce(num);    }    // get/set方法    public int getNum() {        return num;    }    public void setNum(int num) {        this.num = num;    }    public Storage getStorage() {        return storage;    }    public void setStorage(Storage storage) {        this.storage = storage;    }}/** * 消费者类Consumer继承线程类Thread */class Consumer extends Thread {    // 每次消费的产品数量    private int num;    // 所在放置的仓库    private Storage storage;    // 构造函数,设置仓库    public Consumer(Storage storage) {        this.storage = storage;    }    // 线程run函数    public void run() {        consume(num);    }    // 调用仓库Storage的生产函数    public void consume(int num) {        storage.consume(num);    }    // get/set方法    public int getNum() {        return num;    }    public void setNum(int num) {        this.num = num;    }    public Storage getStorage() {        return storage;    }    public void setStorage(Storage storage) {        this.storage = storage;    }}/** * 测试类Test */public class Test {    public static void main(String[] args) {        // 仓库对象        Storage storage = new Storage();        // 生产者对象        Producer p1 = new Producer(storage);        Producer p2 = new Producer(storage);        Producer p3 = new Producer(storage);        Producer p4 = new Producer(storage);        Producer p5 = new Producer(storage);        Producer p6 = new Producer(storage);        Producer p7 = new Producer(storage);        // 消费者对象        Consumer c1 = new Consumer(storage);        Consumer c2 = new Consumer(storage);        Consumer c3 = new Consumer(storage);        // 设置生产者产品生产数量        p1.setNum(20);        p2.setNum(30);        p3.setNum(30);        p4.setNum(10);        p5.setNum(10);        p6.setNum(10);        p7.setNum(80);        // 设置消费者产品消费数量        c1.setNum(30);        c2.setNum(20);        c3.setNum(80);        // 线程开始执行        p1.start();        p2.start();        p3.start();        p4.start();        p5.start();        p6.start();        p7.start();        c1.start();        c2.start();        c3.start();    }}
0 0
原创粉丝点击