帮我分析一下一个问题 比如说,一个篮子里有10个苹果,一共有5个客户端,每个客户端每次只能拿1~2个,直到把篮子的苹果去完. 
5个客户端,好像是平行一样
   5个客户端流量获取完之后,获取的数量没有相加在一起,反而是独立的
   比如,A获取1个,B获取2个,C获取1个,D获取2个,E获取2个
  最后应该是1+2+1+2+2=8 ,剩余2个才对吧
 
现在反而是
A 1 剩余9
B 2 剩余8
C 1 剩余1
D 2 剩余8
E 2 剩余8
 
表现就是 每个客户端对应一个 篮子,各玩各的

解决方案 »

  1.   

    首先你的篮子必须是共有的,如果你的篮子是一个变量,那必须是一个全局的,
    而不能每启动一个线程就去new出一个新的变量,并且在取的时候必须保证线程安全,
    一个线程在取,其它线程就必须等在这里。
    如篮子是数据库,也是一样的
      

  2.   

    public class ShareServer 
    {
    private static List<Socket> list = new LinkedList<Socket>(); // 保存连接对象
    private ExecutorService exec;
    public void start() throws Exception
    {
    ServerSocket serverSocket = new ServerSocket(2000);
    exec = Executors.newCachedThreadPool();
    String xml = "<cross-domain-policy>";
    xml = xml + "<allow-access-from domain=\"*\" to-ports=\"*\" />";
    xml = xml + "</cross-domain-policy>";
    while(true)
    {
    try
    {
    //新建一个连接
    Socket socket = serverSocket.accept();
    list.add(socket);
    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    PrintWriter pw = new PrintWriter(socket.getOutputStream());
    //接收用户名
    char[] by = new char[22];
    br.read(by,0,22);
    String head = new String(by);
    if(head.equals("<policy-file-request/>"))
    {
    pw.print(xml + "\0");
    pw.flush();
    }
    else
    {
    System.out.println("new ServerThread  ");
    ServerThread thread = new ServerThread(socket,list);
    thread.start();
    }
    // System.out.println("------------启动【"+list.size()+"】个线程---------------");
    // exec.execute(new Handler(socket,list));
    System.out.println("连接成功......");

    }
    catch (Exception e)
    {
    System.out.println("服务器出现异常!" + e );
    }

    }
    }
    public static void main(String[] args)
    {
    try
    {
    new ShareServer().start();
    }
    catch (Exception e)
    {
    System.out.println("socket异常:" + e);
    }

    }
    }
    这个是服务端的代码,帮忙看看每启动一个线程就去new出一个新的变量?谢谢啊
      

  3.   

    public class ShareServer 
    {
    private static List<Socket> list = new LinkedList<Socket>(); // 保存连接对象
    private ExecutorService exec;
    public void start() throws Exception
    {
    ServerSocket serverSocket = new ServerSocket(2000);
    exec = Executors.newCachedThreadPool();
    String xml = "<cross-domain-policy>";
    xml = xml + "<allow-access-from domain=\"*\" to-ports=\"*\" />";
    xml = xml + "</cross-domain-policy>";
    while(true)
    {
    try
    {
    //新建一个连接
    Socket socket = serverSocket.accept();
    list.add(socket);
    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    PrintWriter pw = new PrintWriter(socket.getOutputStream());
    //接收用户名
    char[] by = new char[22];
    br.read(by,0,22);
    String head = new String(by);
    if(head.equals("<policy-file-request/>"))
    {
    pw.print(xml + "\0");
    pw.flush();
    }
    else
    {
    System.out.println("new ServerThread  ");
    ServerThread thread = new ServerThread(socket,list);
    thread.start();
    }
    // System.out.println("------------启动【"+list.size()+"】个线程---------------");
    // exec.execute(new Handler(socket,list));
    System.out.println("连接成功......");

    }
    catch (Exception e)
    {
    System.out.println("服务器出现异常!" + e );
    }

    }
    }
    public static void main(String[] args)
    {
    try
    {
    new ShareServer().start();
    }
    catch (Exception e)
    {
    System.out.println("socket异常:" + e);
    }

    }
    }
    这个是服务端的代码,帮忙看看每启动一个线程就去new出一个新的变量?谢谢啊
      

  4.   

    synchronized你的10个苹果,你要理解线程是微观异步,宏观同步这个概念,再怎么同时取,实际还是分时间取得,那么你要做的就是同步这10个苹果,并不是一个线程new10个苹果
      

  5.   

    Apple.javapublic class Apple {
    private int blance; public Apple(int blance) {
    this.blance = blance;
    }
    public synchronized void getApple(int getNum) {
    if (blance >= getNum) {
    System.out.println(Thread.currentThread().getName() + "取出了"
    + getNum + "个苹果");
    blance=blance-getNum;
    System.out.println("还剩下"+blance);
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    public int getBlance() {
    return blance;
    }
    }TestThread.javapublic class TestThread extends Thread {
    private int getNum;
    private Apple apple;
    public TestThread(String name,int getNum,Apple apple){
    super(name);
    this.getNum=getNum;
    this.apple=apple;
    }
    public void run(){
    apple.getApple(getNum);
    }
    public static void main(String[] args){
        Apple apple=new Apple(10);
    new TestThread("A",2,apple).start();
    new TestThread("B",1,apple).start();
    new TestThread("C",1,apple).start();
    new TestThread("D",1,apple).start();
    new TestThread("E",2,apple).start();
    }
    }