java 设计4个线程 两个线程+1 两个线程-1

package com.cg.javabasic;

public class JavaThreadTest2{

	/**
	 * @param args
	 */
	private int j=0;

	public synchronized void inc(){
		j++;
		System.out.println(Thread.currentThread().getName()+" Inc "+ j);
	}
	public synchronized void dec(){
		j--;
		System.out.println(Thread.currentThread().getName()+" Dec "+ j);
	}
	public static void main(String[] args) {
		JavaThreadTest2 t = new JavaThreadTest2();
		Inc inc = t.new Inc();
		Dec dec = t.new Dec();
		for(int i =0;i<2;i++){
			Thread tt = new Thread(inc);
			tt.start();
			tt = new Thread(dec);
			tt.start();
		}
	}

	class Inc implements Runnable{
		public void run(){
			for(int i=0;i<=10;i++){
				inc();
			}
		}
	}
	
	class Dec implements Runnable{
		public void run(){
			for(int i= 0 ;i<=10;i++){
				dec();
			}
		}
	}
}

你可能感兴趣的:(java,thread,J#)