java多线程竞争

import java.util.Timer;
import java.util.TimerTask;

class ComcatFun {// 通信函数
	int flag, flagTemp = 0;
	boolean isRun = false;

	public void isThreadWait(String strCMD, int intTemp) {
		if (flag > 10000) {
			flag = 0;
		}
		System.out.println(strCMD + "--->" + flag);
		flagTemp = flag;
		if (flag % 4 != intTemp) {
			try {
				wait();
			} catch (Exception e) {
			}
		}
		flag++;
		// notifyAll();
		// notify();

	}

	public synchronized void T1(String strTemp) {
		isThreadWait(strTemp, 0);
	}

	public synchronized void T2(String strTemp) {
		isThreadWait(strTemp, 1);
	}

	public synchronized void T3(String strTemp) {
		isThreadWait(strTemp, 2);
	}

	public synchronized void T4(String strTemp) {
		isThreadWait(strTemp, 3);
	}

	public synchronized void threadNotify() {
		// flag++;
		System.out.println("threadNotify");// watch dog
		notify();
	}
};

class runNable1 implements Runnable {
	private ComcatFun per = null;

	public runNable1(ComcatFun per) {
		this.per = per;
	}

	public void run() {
		while (this.per.isRun) {
			per.T1("ES");
			try {
				Thread.sleep(700);
			} catch (Exception e) {
			}
		}
	}
};

class runNable2 implements Runnable {
	private ComcatFun per = null;

	public runNable2(ComcatFun per) {
		this.per = per;
	}

	public void run() {

		try {
			Thread.sleep(1000);
		} catch (Exception e) {
		}

		while (this.per.isRun) {
			per.T2("ME");
			try {
				Thread.sleep(700);
			} catch (Exception e) {
			}
		}
	}
};

class runNable3 implements Runnable {
	private ComcatFun per = null;

	public runNable3(ComcatFun per) {
		this.per = per;
	}

	public void run() {
		try {
			Thread.sleep(2000);
		} catch (Exception e) {
		}
		while (this.per.isRun) {
			per.T3("GETSYN");
			try {
				Thread.sleep(700);
			} catch (Exception e) {
			}
		}
	}
};

class runNable4 implements Runnable {
	private ComcatFun per = null;

	public runNable4(ComcatFun per) {
		this.per = per;
	}

	public void run() {
		try {
			Thread.sleep(3000);
		} catch (Exception e) {
		}
		while (this.per.isRun) {
			per.T4("LKL");
			try {
				Thread.sleep(2000);
			} catch (Exception e) {
			}
		}
	}
};

public class HelloWorld {
	public static Timer mTimer = null;
	public static TimerTask mTimerTask = null;
	static ComcatFun per = new ComcatFun();
	static runNable1 r1 = new runNable1(per);
	static runNable2 r2 = new runNable2(per);
	static runNable3 r3 = new runNable3(per);
	static runNable4 r4 = new runNable4(per);

	// static Thread T1 = new Thread(r1);
	// static Thread T2 = new Thread(r2);
	// static Thread T3 = new Thread(r3);
	// static Thread T4 = new Thread(r4);

	public static void main(String args[]) {
		ThreadStart();
		startTimer();

	}

	public static void ThreadStart() {
		per.isRun = true;
		new Thread(r1).start();
		new Thread(r2).start();
		new Thread(r3).start();
		new Thread(r4).start();
	}

	public static void ThreadStop() {
		per.isRun = false;
	}

	/**************************************************************************
	 * 定时器
	 **************************************************************************/
	private static void startTimer() {
		if (mTimer == null) {
			mTimer = new Timer();
		}

		if (mTimerTask == null) {
			mTimerTask = new TimerTask() {
				@Override
				public void run() {
					// System.out.println("flag-->" + per.flag);// watch dog
					// System.out.println("flagTemp-->" + per.flagTemp);// watch
					// ThreadStop(); // dog
					// ThreadStart();
					if (per.flag - per.flagTemp != 1) {
						System.out.println("xxxxcccccccccccc");// watch dog
						per.threadNotify();
					}
				}
			};
		}

		if (mTimer != null && mTimerTask != null)
			mTimer.schedule(mTimerTask, 5000, 5000);

	}

};

你可能感兴趣的:(java多线程竞争)