线程和同步

时间:2013-01-14 10:24:00

标签: java eclipse thread-synchronization

我需要为以下内容创建一个java程序:

  1. 创建一个ArrayList来存储员工的姓名。
  2. 创建两个同步方法,将员工姓名添加到ArrayList并打印员工姓名。
  3. 一旦线程完成添加员工,就打印员工姓名。
  4. 我做了以下但是没有用。它在“pr.print(X)”行给出了一个例外。有人可以帮忙吗?这不是我的家庭作业!我只是想学习。

    import java.util.*;
    
    public class Ch5Ex2 
     {
      public static void main(String[] args) 
       {
        List<String> li = new ArrayList<String>();
        Print pri = new Print();
        pri.start();
        Insert in = new Insert(li);
        in.start();
       }
    }   
    
    class Insert extends Thread
    {
     Print pr = new Print();
     List<String> x;
     public Insert(List<String> x)
      {
        this.x = x;
      }
    
    public synchronized void run()
     {
        try
        {
          x.add("robin");
          x.add("ravi");
          x.add("raj");
          pr.print(x);
        }
        catch(Exception e)
         {
            e.printStackTrace();
         }
     }
    
    }
    
    class Print extends Thread
    {
     List<String> y;
    public void print(List<String> y)
     {
        this.y = y;
        notify();
     }
    
    public synchronized void run()
     {
        try
        {
         wait();
         for(int i=0;i<y.size();i++)
          {
             System.out.println(y.get(i));
          }
         }
        catch(Exception e)
        {
            e.printStackTrace();
        }
     }
    }   
    

4 个答案:

答案 0 :(得分:2)

我猜prInsert为空,你从不调用构造函数。

答案 1 :(得分:2)

我认为你需要这个:

import java.util.*;

public class Ch5Ex2 {
    public static void main(String[] args) {
        List<String> li = new ArrayList<String>();
        Print pri = new Print();
        pri.start();
        Insert in = new Insert(li, pri);
        in.start();
    }
}


class Insert extends Thread {
    Print pr;
    List<String> x;
    public Insert(List<String> x, Print p) {
        this.x = x;
        pr = p;
    }

    public synchronized void run() {
        try{
            x.add("robin");
            x.add("ravi");
            x.add("raj");
            pr.print(x);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

class Print extends Thread {
    List<String> y;
    public synchronized void print(List<String> y) {
        this.y = y;
        notify();
    }

    public synchronized void run() {
        try {
            while(y == null){
                wait();
            }
            for(int i=0;i<y.size();i++) {
                System.out.println(y.get(i));
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

现在这应该有用......

解决方案是:当您在wait()方法Print threadpri object main()进行notify()时,必须pri object Print thread notify()为了让Print thread继续下去。此外,如果在y开始之前调用Print,则会non-null中的Print thread转为wait()而您的{{1}}将不会{{1}} {{1}}在那种情况下.. :))

答案 2 :(得分:0)

为了让您的生活更轻松,您可能会忘记应用程序级别的同步,只需通过调用此ArrayList来创建同步Collections#synchronizedList()

列出yourArray = Collections.synchronizedList(new ArrayList())

在这种情况下,来自不同线程的所有yourArray调用都将被同步。 除此之外,pr看起来像null,因为你从未实例化它(感谢@ Nikolay Kuznetsov )。

答案 3 :(得分:0)

你没有为pr分配内存。你只是创建一个变量Print pr.So你必须得到NPE