定义一个int count=1000,一个加法线程使count加1,一个减法线程使count减1,要求两个线程分别循环100次(其实多少次都行),并且每个线程连续加或连续减得次数不能超过3次(这个次数也没有限制)!在写这个代码时如何返回线程所处的状态,以判断连加或连减的条件啊!请高手指导!谢谢!!

解决方案 »

  1.   

    package data2009_5_24;public class ThreadTest { /**
     * @param args
     */
    public static void main(String[] args) { new MainDisplay();
    }}class MainDisplay
    {

    public MainDisplay()
    {
    Thread add = new Thread(new PlusRunnable(), "add");
    Thread minus = new Thread(new MinusRunnable(), "minus");
    add.start();
    minus.start();
    }

    class PlusRunnable implements Runnable
    {
    public void run() {

    while (loopCount < MAX_LOOP_COUNT)
    {
    addAction();
    }
    return;
    }

    }

    class MinusRunnable implements Runnable
    { public void run() {

    while (loopCount < MAX_LOOP_COUNT)
    {
    minusAction();
    }

    return;
    }

    }

    private synchronized void addAction()
    {
    if (addCount < MAX_SIGLE_OP_COUNT)
    {
    data++;
    addCount++;
    minusCount = 0;
    loopCount++;
    String theClass = Thread.currentThread().getName();
    System.out.println(theClass + "------" + data + "加一操作");
    }
    }

    private synchronized void minusAction()
    {
    if (minusCount < MAX_SIGLE_OP_COUNT)
    {
    data--;
    minusCount++;
    addCount = 0;
    loopCount++;
    String theClass = Thread.currentThread().getName();
    System.out.println(theClass + "------" + data + "减一操作");
    }
    }
    /*循环最大次数*/
    private final int MAX_LOOP_COUNT = 100;
    /*单个循环连续处理最大次数*/
    private final int MAX_SIGLE_OP_COUNT = 3;

    /*循环次数*/
    private int loopCount;

    /*连续增加计数*/
    private int addCount;
    /*连续相减计数*/
    private int minusCount;

    /*原始数据大小*/
    private int data = 1000;

    }
      

  2.   

    这个程序最后的运行结果应该是1000,因为加运算和减运算都运行了100次,所以data的值应该不变才是啊!但是你的程序每进行一次调试,结果都不一样!
    大侠能否用上wait()-notifyAll()重新编写一下 啊?这是一个练习题!!谢谢~~~
      

  3.   

    这样应该能满足你要求。 
    那个锁必须是静态的,这样才能给两个线程一齐去竞争。
    class ThreadSyn implements Runnable{
    public static final int ORIGINAL_COUNT = 1000;
    public static final int MAX_LOOP = 100;
    public static final int MAX_RUN_COUNT = 3;
    private static int count = ORIGINAL_COUNT;
    private static Object lock = new Object();

    private int delta = 0;
    private int runCount = 0;
    public ThreadSyn(int delta) {
    this.delta = delta;
    }
    @Override
    public void run() {
    synchronized (lock) {
    for (int i = 0; i < MAX_LOOP; i++) {
    if(runCount < MAX_RUN_COUNT){
    runCount++;
    count+= delta;
    System.out.println(Thread.currentThread().getName() + ">>> count = " + (count - delta) + " --> " + count);
    lock.notifyAll(); 
    } else {
    try {
    System.out.println(" ======== " + Thread.currentThread().getName() + " wait" );
    runCount = 0;
    lock.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    lock.notifyAll();
    }
    }

    public static void main(String[] args) {
    ThreadSyn plusThread = new ThreadSyn(1);
    ThreadSyn minusThread = new ThreadSyn(-1);
    new Thread(plusThread,"Plus-Thread").start();
    new Thread(minusThread, "Minus-Thread").start();
    }
    }
      

  4.   

    public class ThreadTest 
    {
    public static void main(String[] args)
    {
    Count c = new Count();
    AddTread AT = new AddTread(c);
    SubTread ST = new SubTread(c);
    AT.start();
    ST.start();
    }
    }class Count
    {
    int count =1000;
    int addCount=1;
    int subCount=1;
    int runCount=0;
    boolean flag=false;
    public synchronized void add()

    count++;
    runCount++;
    if(flag==false)//add在flag==FALSE时候运行
    {
    System.out.println("addCount= "+addCount++ + ","+"count= "+count+","+"runCount= "+runCount);

    if(addCount>3)
    {
    flag=true;
    notify();
    try
    {
    wait();
    }
    catch(InterruptedException e)
    {
    e.printStackTrace();
    }
    addCount=1;
    }
    }
    } public synchronized void sub()
    {
    count--;
    runCount++;
    if(flag==true)//sub是在flag==true时候运行
    {

    System.out.println("subCount= "+subCount++ +","+"count= "+count+","+"runCount= "+runCount);
    if(subCount>3)
    {
    flag=false;
    notify();
    try
    {
    wait();
    }
    catch(InterruptedException e)
    {
    e.printStackTrace();
    }
    subCount=1;
    }
    }

    }
    }class AddTread extends Thread
    {
    Count c;
    public AddTread(Count c)
    {
    this.c=c;
    }
    public synchronized void run()
    {
    while(true)
    {
    c.add();
    if(c.runCount>=102)
    {
    System.out.println("程序运行结束!");
    System.exit(0);
    }
    }

    }

    }class SubTread extends Thread
    {
    Count c;
    public SubTread(Count c)
    {
    this.c = c;
    }
    public synchronized void run()
    {
    while(true)
    {
    c.sub();
    if(c.runCount>=102)
    {
    System.out.println("程序运行结束!");
    System.exit(0);
    }
    }

    }
    }
      

  5.   


    现在应该是你要的public class ThreadTest1 { /**
     * @param args
     */
    public static void main(String[] args) {
    new MainDisplay(); }}class MainDisplay { public MainDisplay() {
    Thread add = new Thread(new PlusRunnable(), "add");
    Thread minus = new Thread(new MinusRunnable(), "minus");
    add.start();
    minus.start();
    } class PlusRunnable implements Runnable {
    public void run() { while (addloopCount < MAX_LOOP_COUNT) {
    addAction();
    }
    return;
    } } class MinusRunnable implements Runnable { public void run() { while (minusloopCount < MAX_LOOP_COUNT) {
    minusAction();
    } return;
    } } private synchronized void addAction() {
    if (addCount < MAX_SIGLE_OP_COUNT) {
    data++;
    addCount++;
    minusCount = 0;
    addloopCount++;
    String theClass = Thread.currentThread().getName();
    System.out.println(theClass + "------" + data + "加一操作");
    try {
    wait(200);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    else{
    notifyAll();
    }
    } private synchronized void minusAction() {
    if (minusCount < MAX_SIGLE_OP_COUNT) {
    data--;
    minusCount++;
    addCount = 0;
    minusloopCount++;
    String theClass = Thread.currentThread().getName();
    System.out.println(theClass + "------" + data + "减一操作");
    try {
    wait(200);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    else
    {
    notifyAll();
    }
    } /* 循环最大次数 */
    private final int MAX_LOOP_COUNT = 100;
    /* 单个循环连续处理最大次数 */
    private final int MAX_SIGLE_OP_COUNT = 3; /* 循环次数 */
    private int addloopCount;

    private int minusloopCount; /* 连续增加计数 */
    private int addCount;
    /* 连续相减计数 */
    private int minusCount; /* 原始数据大小 */
    private int data = 1000;}