多线程上机题_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 多线程上机题

多线程上机题

 2014/4/9 0:31:22  beijishiqidu  程序员俱乐部  我要评论(0)
  • 摘要:题目如下:四个线程1,2,3,4.线程1,2对变量i加一.线程3,4对变量i减去一.四个线程顺序执行,每个线程每次只执行一次.i的初始值为0,打印结果01210121012...packagetest01;importjava.util.concurrent.LinkedBlockingQueue;publicclassThreadUtil{privateLinkedBlockingQueue<Integer>lbq=newLinkedBlockingQueue<
  • 标签:多线程 线程
题目如下:
四个线程1,2,3,4. 线程1,2对变量i加一. 线程3,4对变量i减去一.四个线程顺序执行, 每个线程每次只执行一次.i的初始值为0, 打印结果0 1 2 1 0 1 2 1 0 1 2...

class="java">package test01;

import java.util.concurrent.LinkedBlockingQueue;

public class ThreadUtil {

	private LinkedBlockingQueue<Integer> lbq = new LinkedBlockingQueue<Integer>(
			4);

	private int count = 0;

	@SuppressWarnings("boxing")
	public ThreadUtil() {
		lbq.offer(1);
		lbq.offer(2);
		lbq.offer(3);
		lbq.offer(4);
	}

	@SuppressWarnings("boxing")
	public synchronized void inc(int content) {
		while (true) {
			int temp = lbq.peek();
			if (temp == content) {
				break;
			}
			notifyAll();
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.print(count + " ");
		count++;
		try {
			lbq.offer(lbq.take());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		//print();
	}

	@SuppressWarnings("boxing")
	public synchronized void dec(int content){
		while (true) {
			int temp = lbq.peek();
			if (temp == content) {
				break;
			}
			notifyAll();
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.print(count + " ");
		count--;
		try {
			lbq.offer(lbq.take());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		//print();
	}
	
	private void print()
	{
		System.out.println("=====>"+lbq.toString());
	}

}

package test01;

public class IncThread implements Runnable {

	private ThreadUtil tu;

	private int content;

	public IncThread(ThreadUtil tu, int content) {
		this.tu = tu;
		this.content = content;
	}

	@Override
	public void run() {
		while (true) {
			tu.inc(content);
		}
	}

}

package test01;

public class DecThread implements Runnable{

	private ThreadUtil tu;
	
	private int content;

	public DecThread(ThreadUtil tu,int content) {
		this.tu = tu;
		this.content = content;
	}
	
	@Override
	public void run() {
		while (true) {
			tu.dec(content);
		}
	}

}

package test01;

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

public class Test01 {

	public static void main(String[] args) {
		ThreadUtil tu = new ThreadUtil();

		ExecutorService exec = Executors.newFixedThreadPool(4);

		exec.submit(new IncThread(tu, 1));
		exec.submit(new IncThread(tu, 2));
		exec.submit(new DecThread(tu, 3));
		exec.submit(new DecThread(tu, 4));

		exec.shutdown();

	}

}

运行结果如下:
0 1 2 1 0 1 2 1 0 1 2 1 0 1 2 1 

参考自:http://ethanzhou.blog.51cto.com/6147883/1045683
上一篇: 巨人或推《中国好声音》同名手游 下一篇: 没有下一篇了!
发表评论
用户名: 匿名