java多线程共享对象切换标志

      直接上题:

      要求创建三个线程,输出1-90, 最开始第一个线程输出1-5,第二个输出6-15,第三个输出16-30
 接着再第一个线程输出31-35, 再第二个36-45,再第三个46-60...就这样循环下去,直到打印出90个数。

 与互联网其他解决输出 1~75有差异,并且有些是错误,线程不安全的,这里进行常规方法改进。

jdk版本提升7,8后,synchronized大幅得以提升。

      解决办法:通俗处理方式,简单易理解,使用共享对象,使用标志位转移线程状态,比较灵活。

 

package com.dennis.study.thread;

/**
 * 要求创建三个线程,输出1-90, 最开始第一个线程输出1-5,第二个输出6-15,第三个输出16-30
 * 接着再第一个线程输出31-35, 再第二个36-45,再第三个46-60...就这样循环下去,直到打印出90个数
 * 解决办法:通俗处理方式,简单易理解,使用共享对象,使用标志位转移线程状态,比较灵活。
 * 
 * @author dennis
 * 
 */
public class Thread3_1to90 {

	static ShareData3_ sd = new ShareData3_(1);
	
	public static void main(String[] args) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				sd.print1();
			}
		}).start();
		new Thread(new Runnable() {
			@Override
			public void run() {
				sd.print2();
			}
		}).start();
		new Thread(new Runnable() {
			@Override
			public void run() {
				sd.print3();
			}
		}).start();
		while (Thread.activeCount() > 0) {
			Thread.currentThread().setDaemon(true);
		}
	}
}

class ShareData3_ {

	private static int num = 0;
	private volatile int flag = 1;
	private final static int end = 90; 
	
	public ShareData3_( int f) {
		this.flag = f;
	}

	/**
	 * 1~5 31~35,61~65,
	 */
	public synchronized void print1() {
		while(num < end) {
			while(flag > 1) {
				wait_();
			}
			while (num % 30 < 5 && num < end) {
				for (int i = 0; i < 5; i++) {
					System.out.println("print1 = " + Thread.currentThread().getName() + "\t" + ++num);
				}
			}
			sleep_();
			flag  = 2;
			notifyAll();
		}
	}

	

	/**
	 * 6~15 36~45,66~75,
	 */
	public synchronized void print2() {
		while(num <end) {
			while(flag != 2) {
				wait_();
			}
			while (num % 30 < 15 && num % 30 > 4  && num < end) {
				for (int i = 0; i < 10; i++) {
					System.out.println("print2 = " + Thread.currentThread().getName() + "\t" + ++num);
				}
			}
			sleep_();
			flag  = 3;
			notifyAll();
		}
	}
	
	/**
	 * 16~30 46~60,76~90,
	 */
	public synchronized void print3() {
		while(num < end) {
			while(flag < 3) {
				wait_();
			}
			while (num % 30 > 14  && num < end) {
				for (int i = 0; i < 15; i++) {
					System.out.println("print3 = " + Thread.currentThread().getName() + "\t" + ++num);
				}
			}
			sleep_();
			flag  = 1;
			notifyAll();
		}
	}
	
	private final void sleep_() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private final void wait_() {
		try {
			wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
}

 记录使用 ^_^

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值