zoukankan      html  css  js  c++  java
  • 一个java多线程实例

    import java.util.List;
    import java.util.ArrayList;
    import java.util.Queue;
    import java.util.LinkedList;
    public class Test3 {
        public static void main(String[] args) {
            final Manager3 m3 = new Manager3();
            m3.init();
            Thread t1 = new Thread(new Runnable() {
                public void run() {
                    while (true) {
                        if (m3.getter.q.size() > 0) {
                            TestData3 td = m3.getter.q.pop();
                            System.out.println(td.getName());
                        } else {
                            try {wait();} catch (Exception e) {}
                        }
                    }
                }
            });
            t1.start();
        }
    }
    class TestData3 {
        String type;
        String id;
        String name;
        TestData3(String type, String id, String name) {
            this.type = type;
            this.id = id;
            this.name = name;
        }
        String getName() {
            return name;
        }
    }
    class TestDataQueue3 {
        Queue<TestData3> q = new LinkedList<TestData3>();
        synchronized void push(TestData3 td) {
            q.offer(td);
        }
        synchronized TestData3 pop() {
            return q.poll();
        }
        long size() {
            return q.size();
        }
    }
    class Manager3 {
        Getter3 getter = new Getter3();
        Setter3 setter = new Setter3();
        Adapter3 adapter = Setter3.adapter;
        void init() {
            adapter.addGetter(getter);
            setter.regist();
            setter.putData();
            adapter.listenData();
        }
    }
    class Getter3 {
        TestDataQueue3 q = new TestDataQueue3();
        void update(TestData3 data) {
            q.push(data);
        }
    }
    class Adapter3 {
        TestDataQueue3 q = new TestDataQueue3();
        List<Getter3> glst = new ArrayList<Getter3>();
        List<Setter3> slst = new ArrayList<Setter3>();
        void addGetter(Getter3 g) {
            glst.add(g);
        }
        void addSetter(Setter3 s) {
            slst.add(s);
        }
        void listenData() {
            Thread t = new Thread(new Runnable() {
                public void run() {
                    while (true) {
                        if (q.size() > 0) {
                            broadcast(q.pop());
                        } else {
                            try {
                                wait();
                            } catch (Exception e) {}
                        }
                    }
                }
            });
            t.start();
        }
        void broadcast(TestData3 td) {
            for (int i = 0; i < glst.size(); i++) {
                glst.get(i).update(td);
            }
        }
    }
    class Setter3 {
        static final Adapter3 adapter = new Adapter3();
        void regist() {
            adapter.addSetter(this);
        }
        void putData() {
            Thread t = new Thread(new Runnable() {
                public void run() {
                    int i = 0;
                    while (true) {
                        TestData3 td = new TestData3("computer", "c" + String.format("%08d", i), "computer" + i);
                        adapter.q.push(td);
                        i++;
                        try {
                            Thread.sleep(1000);
                            notify();
                        } catch (Exception e) {}
                    }
                }
            });
            t.start();
        }
    }
  • 相关阅读:
    鳥哥的 Linux 私房菜——第十三章、学习 Shell Scripts(转发)(未完待续)
    鳥哥的 Linux 私房菜——第十六章、例行性工作排程 (crontab) (转发)(未完待续)
    RT-Thread ------ event 事件
    sscanf() ------ 获取字符串中的参数
    燃气热水器的调节
    Adobe Illustrator CC ------ AI脚本插件合集
    你真的理解CSS的linear-gradient?
    IDEA中Grep Console插件的安装及使用
    Windows下删除以.结尾文件夹的方法
    lwip库的发送和接收函数
  • 原文地址:https://www.cnblogs.com/feilv/p/4220361.html
Copyright © 2011-2022 走看看