• 那是从何处传来的钟声呢?偶尔听到那钟声,平添一份喜悦与向往之情。

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

后端 Nanait 12年前 (2012-08-07) 1022次浏览 已收录 0个评论 扫描二维码

首先给出题目要求

甲线程输出: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. }

明显,方法一的代码更美观,通用。

 


何处钟 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:java 三个线程同步的两种方法
喜欢 (0)
[15211539367@163.com]
分享 (0)

您必须 登录 才能发表评论!