java三个线程同步的两种方法

avatar 2017年05月29日20:12:10 1 1730 views
博主分享免费Java教学视频,B站账号:Java刘哥
首先给出题目要求
甲线程输出:A、B、C、D、E 乙线程输出:1、2、3、4、5 丙线程数出:甲、乙、丙、丁、戊 最终输出结果为(注:这是唯一可能的结果) A 1 甲 B 2 乙 C 3 丙 D 4 丁 E 5 戊
无非都是同步,消费者生产者的例子比较好,实现方法也是各有不同,这里自己整理了两种方法,做个笔记。 方法一代码
  1. package com.liuyanzhao;
  2. /*
  3.  *
  4.  * @author WellsLiu
  5.  *
  6.  */
  7. class MySignal {
  8.     int data = 0;
  9. }
  10. class MyThread implements Runnable {
  11.     MySignal s = new MySignal();
  12.     char c[];
  13.     int runFlag;
  14.     int nextFlag;
  15.     public MyThread(MySignal s, char[] c, int x, int y) {
  16.         this.s = s;
  17.         this.c = c;
  18.         runFlag = x;
  19.         nextFlag = y;
  20.     }
  21.     public void run() {
  22.         synchronized (s) {
  23.             for (int i = 0; i < c.length; i++) {
  24.                 while (s.data != runFlag) {
  25.                     try {
  26.                         s.wait();
  27.                     } catch (InterruptedException e) {
  28.                         e.printStackTrace();
  29.                     }
  30.                 }
  31.                 System.out.print(c[i] + " ");
  32.                 s.data = nextFlag;
  33.                 s.notifyAll();
  34.             }
  35.         }
  36.     }
  37. }
  38. public class Test {
  39.     public static void main(String[] args) {
  40.         MySignal s = new MySignal();
  41.         char[] c1 = { 'A', 'B', 'C', 'D', 'E' };
  42.         char[] c2 = { '1', '2', '3', '4', '5' };
  43.         char[] c3 = { '甲', '乙', '丙', '丁', '戊' };
  44.         Thread t1 = new Thread(new MyThread(s, c1, 01));
  45.         Thread t2 = new Thread(new MyThread(s, c2, 12));
  46.         Thread t3 = new Thread(new MyThread(s, c3, 20));
  47.         t1.start();
  48.         t2.start();
  49.         t3.start();
  50.     }
  51. }
  方法二代码
  1. package com.liuyanzhao;
  2. /*
  3.  *
  4.  * @author WellsLiu
  5.  *
  6.  */
  7. class Resource {
  8.     int flag=1;
  9. }
  10. class Thread1 extends Thread {
  11.     Resource res;
  12.     public Thread1(Resource res) {
  13.         this.res = res;
  14.     }
  15.     int i=0;
  16.     char []c = {'A', 'B', 'C', 'D', 'E'};
  17.     public  void run() {
  18.         synchronized (res) {
  19.             while(i<5) {
  20.                 while(res.flag!=1) {
  21.                     try {
  22.                         res.wait();//t1
  23.                     } catch (InterruptedException e) {
  24.                         e.printStackTrace();
  25.                     }
  26.                 }
  27.                 System.out.print(c[i]+" ");
  28.                 i++;
  29.                 res.notifyAll();
  30.                 res.flag=2;
  31.             }
  32.         }
  33.     }
  34. }
  35. class Thread2 extends Thread {
  36.     Resource res;
  37.     public Thread2(Resource res) {
  38.         this.res = res;
  39.     }
  40.     int i=0;
  41.     char []c = {'1', '2', '3', '4', '5'};
  42.     public  void run() {
  43.         synchronized (res) {
  44.             while(i<5) {
  45.                 while(res.flag!=2) {
  46.                     try {
  47.                         res.wait();
  48.                     } catch (InterruptedException e) {
  49.                         e.printStackTrace();
  50.                     }
  51.                 }
  52.                 System.out.print(c[i]+" ");
  53.                 i++;
  54.                 res.notifyAll();
  55.                 res.flag=3;
  56.             }
  57.         }
  58.     }
  59. }
  60. class Thread3 extends Thread {
  61.     Resource res;
  62.     public Thread3(Resource res) {
  63.         this.res = res;
  64.     }
  65.     char []c = {'甲','乙','丙','丁','戊'};
  66.     public  void run() {
  67.         synchronized (res) {
  68.             for(int i=0;i<c.length;i++){
  69.                 while(res.flag!=3) {
  70.                     try {
  71.                         res.wait();
  72.                     } catch (InterruptedException e) {
  73.                         e.printStackTrace();
  74.                     }
  75.                 }
  76.                 System.out.print(c[i]+" ");
  77.                 res.notifyAll();
  78.                 res.flag=1;
  79.             }
  80.         }
  81.     }
  82. }
  83. public class Test1 {
  84.     public static void main(String[] args) {
  85.         Resource res = new Resource();
  86.         Thread1 t1 = new Thread1(res);
  87.         Thread2 t2 = new Thread2(res);
  88.         Thread3 t3 = new Thread3(res);
  89.         t1.start();
  90.         t2.start();
  91.         t3.start();
  92.     }
  93. }
明显,方法一的代码更美观,通用。   本文链接:https://liuyanzhao.com/4501.html
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0