Thread common example

1.设计 4 个线程,其中两个线程每次对 j增加1,另外两个线程对j每次减少1。

public class ThreadTest {
	public static void main(String[] args) {
		MyThread thread = new MyThread();

		for (int i = 0; i < 2; i++) {
			Thread inc = new Thread(new Inc(thread));
			Thread dec = new Thread(new Dec(thread));

			inc.start();
			dec.start();
		}
	}
}

class MyThread {
	private int j;

	public synchronized void dec() {
		j--;
		System.out.println(Thread.currentThread().getName() + "-dec:" + j);
	}

	public synchronized void inc() {
		j++;
		System.out.println(Thread.currentThread().getName() + "-inc:" + j);
	}
}

class Dec implements Runnable {

	private MyThread obj;

	public Dec(MyThread obj) {
		this.obj = obj;
	}

	@Override
	public void run() {
		this.obj.dec();
	}
}

class Inc implements Runnable {

	private MyThread obj;

	public Inc(MyThread obj) {
		this.obj = obj;
	}

	@Override
	public void run() {
		this.obj.inc();
	}
}
 

2.设计两个线程,第一个线程从1加到10,在这加的过程中,第二个线程必须等待,当第一个线程加到10时,第二个线程启动,从10减到1,在这减的过程中,第一个线程必须等待,循环执行若干时间后,退出程序。

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadTest {
	public static void main(String[] args) throws Exception {
		final Num num = new Num();
		ExecutorService exec = Executors.newCachedThreadPool();
		exec.execute(new Runnable() {

			@Override
			public void run() {
				try {
					while (!Thread.interrupted()) {
						TimeUnit.MILLISECONDS.sleep(200);
						num.increased();
						num.waitForDecreasing();
					}
				} catch (InterruptedException e) {
					System.out.println("Exiting via interrupt");
				}
				System.out.println("Ending Increase");

			}
		});

		exec.execute(new Runnable() {

			@Override
			public void run() {
				try {
					while (!Thread.interrupted()) {
						num.waitForIncreasing();
						TimeUnit.MILLISECONDS.sleep(200);
						num.decreased();
					}
				} catch (InterruptedException e) {
					System.out.println("Exiting via interrupt");
				}
				System.out.println("Ending Decrease");

			}
		});

		TimeUnit.SECONDS.sleep(5); // Run for a while...
		exec.shutdownNow(); // Interrupt all tasks
	}
}

class Num {
	private boolean flag = false;
	private int i;

	public synchronized void increased() {
		flag = true;

		for (i = 1; i < 11; i++) {
			System.out.println(Thread.currentThread().getName() + "-inc " + i);
		}

		notifyAll();
	}

	public synchronized void decreased() {
		flag = false;

		for (i = 10; i > 0; i--) {
			System.out.println(Thread.currentThread().getName() + "-dec " + i);
		}

		notifyAll();
	}

	public synchronized void waitForIncreasing() throws InterruptedException {
		while (flag == false)
			wait();
	}

	public synchronized void waitForDecreasing() throws InterruptedException {
		while (flag == true)
			wait();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值