2017-04-03 100 views
0

任务,当我试图使用打印ABC反复使用三个线程无限次数 我的代码是多线程在Java中使用3个一线

package javap; 

public class Pattern { 

    volatile int status=1; 
    public static void main(String[] args) { 

     Pattern p = new Pattern(); 

     A1 a=new A1(p); 
     B1 b=new B1(p); 
     C1 c=new C1(p); 

     a.start(); 
     b.start(); 
     c.start(); 
    } 
} 

class A1 extends Thread{ 
    Pattern p1; 

    A1(Pattern p){ 
     this.p1 = p; 
    } 

    @Override 
    public void run() { 

     try{ 
      synchronized (p1) { 

       for (int i = 0; i < 100; i++) { 

        while(p1.status!=1){ 
         p1.wait(); 
        } 

        System.out.print("A "); 
        p1.status = 2; 
        p1.notifyAll(); 
       } 

      } 
     }catch (Exception e) { 
      System.out.println("Exception 1 :"+e.getMessage()); 
     } 

    } 

} 

class B1 extends Thread{ 

    Pattern p2; 

    B1(Pattern p2){ 
     this.p2 = p2; 
    } 

    @Override 
    public void run() { 

     try{ 
      synchronized (p2) { 

       for (int i = 0; i < 100; i++) { 

        while(p2.status!=2){ 
         p2.wait(); 
        } 

        System.out.print("B "); 
        p2.status = 3; 
        p2.notifyAll(); 
       } 

      } 
     }catch (Exception e) { 
      System.out.println("Exception 2 :"+e.getMessage()); 
     } 

    } 
} 


class C1 extends Thread{ 

    Pattern p3; 

    C1(Pattern p){ 
     this.p3 = p; 
    } 

    @Override 
    public void run() { 

     try{ 
      synchronized (p3) { 

       for (int i = 0; i < 100; i++) { 

        while(p3.status!=3){ 
         p3.wait(); 
        } 

        System.out.print("C "); 
        p3.status = 1; 
        p3.notifyAll(); 
       } 

      } 
     }catch (Exception e) { 
      System.out.println("Exception 3 :"+e.getMessage()); 
     } 

    } 
} 

打印ABC(;;)或同时(真)我的ide挂起,我nt得到任何输出 ,所以我限制它100次。 有无论如何,我可以让这个运行无数次。

在此先感谢

+0

'为(;;的System.out.println())Stream.of( “A”, “B”, “C” ).parallel()forEachOrdered(的System.out ::打印);' –

+0

我应该repllace所有与此@elliott弗里希 – aish

+0

将取代你写的一切循环。它并行打印“a”,然后“b”,然后“c”,然后打印一个换行符并循环。 –

回答

0

我已修改了逻辑一点,并且它获得输出流A B C为1000毫秒(用于可视化更好)的间隔。

public class Pattern { 

    volatile int status = 1; 

    public static void main(String[] args) { 

     Pattern p = new Pattern(); 

     A1 a = new A1(p); 
     B1 b = new B1(p); 
     C1 c = new C1(p); 

     a.start(); 
     b.start(); 
     c.start(); 
    } 
} 

class A1 extends Thread { 
    Pattern p1; 

    A1(Pattern p) { 
     this.p1 = p; 
    } 

    @Override 
    public void run() { 

     try { 
      synchronized (p1) { 
       while (true) { 
        while (p1.status != 1) { 
         try { 
          p1.wait(); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
        } 
        System.out.print("A "); 
        p1.status = 2; 
        p1.notifyAll(); 
       } 
      } 
     } catch (Exception e) { 
      System.out.println("Exception 1 :" + e.getMessage()); 
     } 

    } 

} 

class B1 extends Thread { 

    Pattern p2; 

    B1(Pattern p2) { 
     this.p2 = p2; 
    } 

    @Override 
    public void run() { 

     try { 
      synchronized (p2) { 
       while (true) { 
        while (p2.status != 2) { 
         try { 
          p2.wait(); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
        } 
        System.out.print("B "); 
        p2.status = 3; 
        p2.notifyAll(); 
       } 
      } 
     } catch (Exception e) { 
      System.out.println("Exception 2 :" + e.getMessage()); 
     } 

    } 
} 

class C1 extends Thread { 

    Pattern p3; 

    C1(Pattern p) { 
     this.p3 = p; 
    } 

    @Override 
    public void run() { 

     try { 
      synchronized (p3) { 
       while (true) { 
        while (p3.status != 3) { 
         try { 
          p3.wait(); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
        } 
        System.out.print("C "); 
        Thread.sleep(1000); 
        p3.status = 1; 
        p3.notifyAll(); 
       } 
      } 
     } catch (Exception e) { 
      System.out.println("Exception 3 :" + e.getMessage()); 
     } 

    } 
} 

输出:ABCABCABCABCABC .....

1

您的代码似乎罚款。这就是说,看起来你正在编写太多的代码来实现你的目标......考虑使用Executors.newFixedThreadPool(3)来创建3个线程并将它们放入池中。

ExecutorService service = Executors.newFixedThreadPool(3); 

然后,传递一个任务池(可赎回)

Future<String> resultA = service.submit(() -> { 
    System.out.print("A "); 
    return "A"; 
}); 

现在等待任务路过的下一个任务之前完成:

resultA.get(); 

这是一个完整的代码片段:

public static void main(String[] args) throws ExecutionException, InterruptedException { 

    ExecutorService service = Executors.newFixedThreadPool(3); 
    int x = 0; 
    while (x < 100) { 
     service.submit(() -> { 
      System.out.print("A "); 
      return "A"; 
     }).get(); 

     service.submit(() -> { 
      System.out.print("B "); 
      return "B"; 
     }).get(); 

     service.submit(() -> { 
      System.out.print("C "); 
      return "C"; 
     }).get(); 

     x++; 
    } 

    service.shutdown(); 
} 

那么说你可以删除所有的重复代码使用:

public class Main { 
    public static void main(String[] args) throws ExecutionException, InterruptedException { 

     ExecutorService service = Executors.newFixedThreadPool(3); 
     int x = 0; 
     while (x < 100) { 
      service.submit(getTask("A ")).get(); 
      service.submit(getTask("B ")).get(); 
      service.submit(getTask("C ")).get(); 
      x++; 
     } 

     service.shutdown(); 
    } 

    private static Callable<String> getTask(String task) { 
     return() -> { 
      System.out.print(task); 
      return task; 
     }; 
    } 
} 

请注意,主不能抛出异常,但这应该是显而易见的。

此外,@Elliott弗里希答案是真棒,因为它使用JAVA 8并行流。
我想你想了解线程,所以我的答案使用执行人的API,它使线程相关的代码更短,更简洁。

每个人都应该熟悉两个流和执行人。