java多线程竞争

来源:互联网 发布:人工智能公司 编辑:程序博客网 时间:2024/06/05 06:36
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 dognotify();}};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() {@Overridepublic 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 dogper.threadNotify();}}};}if (mTimer != null && mTimerTask != null)mTimer.schedule(mTimerTask, 5000, 5000);}};

0 0
原创粉丝点击