同时运行两个线程

时间:2015-11-10 07:41:06

标签: java multithreading concurrency thread-safety thread-synchronization

我正在尝试两个同时打印两个线程的名称。任何人都可以建议,我做错了什么?我得到" IllegalMonitorStateException

// ODD THREAD RUNNABLE     公共类OddThread实现了Runnable {

    MainClass obj;
    public OddThread(MainClass obj) {
        // TODO Auto-generated constructor stub
        this.obj= obj;
    }
    public void run() {
        int number=1;
        while(number <10)
        {
          obj.PrintNumbers();
          notifyAll();
          try {
            wait();
        } catch (InterruptedException e) {System.out.println("Exception in wait of OddThread");
        }
          number = number+2;

        }

    }

}

//甚至THREAD RUNNABLE

public class EvenThread implements Runnable{

    MainClass obj;
    public EvenThread(MainClass obj)
    {
        this.obj=obj;
    }
    public void run() {
        int number=0;
        while(number<=10)
        {
          obj.PrintNumbers();
          notifyAll();
          try {
            wait();
        } catch (InterruptedException e) {
            System.out.println("Exception in wait of EvenThread");
        }
       number = number+2;
        }

    }

}

// MAIN CLASS

public class MainClass {
   public static void main(String[] args)
   {
       MainClass obj = new MainClass();
      // Boolean flag= true;
       EvenThread evenThread = new EvenThread(obj);
       OddThread oddThread = new OddThread(obj);
       Thread Even = new Thread(evenThread);
       Thread Odd = new Thread(oddThread);
       Even.setName("Even Thread");
       Odd.setName("Odd Thread");
       Even.start();
       Odd.start();
   }

   public synchronized void PrintNumbers()
   {
       System.out.println(Thread.currentThread().getName());
   }

}

我得到的输出是 连线程 奇怪的线程 线程中的异常&#34;偶数线程&#34;线程中的异常&#34;奇数线程&#34; java.lang.IllegalMonitorStateException

2 个答案:

答案 0 :(得分:2)

你应该围绕#include<iostream.h> #include<string.h> #include<stdio.h> #include<fstream.h> void main() { char name[24]; cout << "enter string :"; gets(name); ofstream fout; fout.open("bin_data",ios::out|ios::binary); fout.write((char*)&name,10); fout.close(); } notifyAll() wait() 像:

synchronized (this) {}

synchronized (this) {
    notifyAll();
}

这些方法synchronized (this) { wait(); } notify()notifyAll()只能由作为此对象监视器所有者的线程调用,因此您必须将它们包含在同步中阻止,正如所说Here the Object Java doc

答案 1 :(得分:0)

这是正确的代码:

public class MainClass {
    public static int number=0;
   public static void main(String[] args)
   {
       MainClass obj = new MainClass();
      // Boolean flag= true;
       EvenThread evenThread = new EvenThread(obj);
       OddThread oddThread = new OddThread(obj);
       Thread Even = new Thread(evenThread);
       Thread Odd = new Thread(oddThread);
       Even.setName("Even Thread");
       Odd.setName("Odd Thread");
       Even.start();
       Odd.start();
   }

   public synchronized void PrintNumbers()
   {  
       while(number<20)
       {
           System.out.println(number+"---"+Thread.currentThread().getName());
           number++;
           notifyAll();

           try {
            wait();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

       }
   }

}




public class OddThread implements Runnable {


    MainClass obj;
    public OddThread(MainClass obj) {
        // TODO Auto-generated constructor stub
        this.obj= obj;
    }
    public void run() {

          obj.PrintNumbers();


    }

}



public class EvenThread implements Runnable{

    MainClass obj;
    public EvenThread(MainClass obj)
    {
        this.obj=obj;
    }
    public void run() {
        int number=0;

          obj.PrintNumbers();


    }

}