java 线程 竞争_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);

}

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值