日期:2014-05-20  浏览次数:20688 次

java多线程队列问题
Java code

package com.wk.demo.myDuilie;

import java.util.Vector;

public class Test implements Runnable {

    public static void main(String[] args) {
        CreateName createName = new CreateName();
        Manager manager = new Manager();
        Test shengren = new Test();
        Thread managerThread = new Thread(manager);
        Thread createNameThread = new Thread(createName);
        createNameThread.start();
        managerThread.start();
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(shengren);
            thread.start();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void run() {
        Person person = new Person();
        Manager.addPerson(person);
        say(person);
    }

    public void say(Person person) {
        while (person.getName() == null) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(person.getName());
    }

}

class Person {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

class CreateName implements Runnable {
    private void createName() {
        Manager.addName(getName());
    }
    private String getName() {
        return "www" + (int) (Math.random() * 100);
    }
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            createName();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Manager implements Runnable {
    private static Vector<Person> persons = new Vector<Person>();
    private static Vector<String> names = new Vector<String>();
    public static void addPerson(Person person) {
        synchronized (persons) {
            persons.insertElementAt(person, persons.size());
        }
    }
    public static void addName(String name) {
        synchronized (names) {
            names.insertElementAt(name, names.size());
        }
    }
    @Override
    public void run() {
        while (true) {
            if (persons.size() > 0 && names.size() > 0) {
                System.out.println("现在有多少个人需要取名:" + persons.size());
                System.out.println("现在有多少个名字:" + names.size());
                persons.remove(0).setName(names.remove(0));
            } else {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


我想问几个问题:
1.队列操作可是这么模拟的吗?
2.比如说name是连接池中的连接,person是高并发的访问用户,这样做合理吗?
3.Manager 中的 while(true) 好吗,可以用别的方法代替吗?

------解决方案--------------------
Java code

class Manager implements Runnable {
    private static BlockingQueue<Person> persons = new LinkedBlockingQueue<Person>();
    private static BlockingQueue<String> names = new LinkedBlockingQueue<String>();
    private static boolean flag = true;
    public static void addPerson(Person person) {
        try {
            persons.put(person);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void addName(String name) {
        try {
            names.put(name);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (flag) {
            System.out.println("现在有多少个人需要取名:" + persons.size());
            System.out.println("现在有多少个名字:" + names.size());
            try {
                persons.take().setName(names.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void exit()
    {
        persons.clear();
        names.clear();
        flag = false;
        addPerson(new Person());
        addName("");
    }
}