如何按线程1打印一半数字,按线程2打印第二一半

时间:2018-06-26 12:37:04

标签: java multithreading

我编写了一个程序,使用2个线程打印5个数字。

public class Class1 {

static int value = 6;
 public void show() {
    for (int i = 0; i < value; value--) {
        System.out.println(Thread.currentThread().getName() + " - Number: " + value);
    }
} 
public static void main(String[] args) {
    final Class1 main = new Class1();

    Runnable runner = new Runnable() {
        @Override
        public void run() {
            main.show();
        }
    };

    new Thread(runner, "Thread 1").start();
    new Thread(runner, "Thread 2").start();
}   }

它给了我这样的输出:

Thread 1 - Number: 6
Thread 2 - Number: 6
Thread 1 - Number: 5
Thread 2 - Number: 4
Thread 1 - Number: 3
Thread 1 - Number: 1
Thread 2 - Number: 2

但是我希望它给我这样的输出

Thread 1 - Number: 6
Thread 1 - Number: 5
Thread 1 - Number: 4
Thread 2 - Number: 3
Thread 2 - Number: 2
Thread 2 - Number: 1

我知道我们很容易在show()方法中使用 if else条件,但是我不想在show()中使用if 方法,我想更改main()方法中的某些内容,以完成工作。

4 个答案:

答案 0 :(得分:1)

第一个线程完成后,您可以等待启动第二个线程,基本上,您需要同步线程执行。

为此,您可以:

  • 输入具有相同密钥的synchronized
  • 在同一锁上使用wait notify关键字
  • 使用Java Lock
  • 在第一个线程上使用join以等待他完成操作,然后再开始第二个线程
  • 使用Observable协调线程的开始

还有许多其他人。

答案 1 :(得分:1)

先解决问题。您没有在线程中使用同步,并且如果要使用join(),则需要在运行方法和

中使用if
static int value = 6;

不是线程安全的!为了更好地使用 AtomicInteger 并控制theads检查 ExecutorService

答案 2 :(得分:1)

您可以按照以下方式进行操作:

final int number = 6;
    Thread t1 = new Thread(new Runnable() {
        @Override
        public void run() {
            for(int i = 0; i< number/2;i++){
                System.out.println("Thread 1 Number: "+i);
            }
        }
    });
    Thread t2 = new Thread(new Runnable() {

        @Override
        public void run() {
            for(int i = number/2; i< number;i++){
                System.out.println("Thread 2 Number: "+i);
            }
        }
    });
        t1.run();
        t2.run();

答案 3 :(得分:0)

public class PrintHalfNumber {

  public static void main(String[] args) {

    Printer sp = new Printer();

    ExecutorService executor = Executors.newFixedThreadPool(2);
    executor.submit(new SecondHalfProducer(sp, 10));
    executor.submit(new FirstHalfProducer(sp , 10));
    executor.shutdown();
  }

}


class Printer {

  Semaphore secondHalf = new Semaphore(0);
  Semaphore firstHalf = new Semaphore(1);

  public void printSecondHalf(int num) {
    try {
        secondHalf.acquire();
    }catch(InterruptedException exception) {

    }
    for(int i = (num/2) + 1 ; i <= num ; i++ ) {
        System.out.println("Thread 2 - Number: " + i);
    }

    firstHalf.release();
  }

  public void printFirstHalf(int num) {
    try {
        firstHalf.acquire();
    }catch(InterruptedException exception) {

    }
    for(int i = 1 ; i <= num/2 ; i++) {
        System.out.println("Thread 1 - Number: " + i);
    }

        secondHalf.release();
  }

}

class SecondHalfProducer implements Runnable {

  Printer sp;
  int index;

  SecondHalfProducer(Printer sp , int index) {
    this.sp = sp;
    this.index = index;
  }

  @Override
  public void run() {
    sp.printSecondHalf(index);
  }
}

class FirstHalfProducer implements Runnable{
  Printer sp;
  int index;

  FirstHalfProducer(Printer sp , int index) {
    this.sp = sp;
    this.index = index;
  }

  @Override
  public void run() {
        sp.printFirstHalf(index);
  }
}

程序输出为:

Thread 1 - Number: 1

Thread 1 - Number: 2

Thread 1 - Number: 3

Thread 1 - Number: 4

Thread 1 - Number: 5

Thread 2 - Number: 6

Thread 2 - Number: 7

Thread 2 - Number: 8

Thread 2 - Number: 9

Thread 2 - Number: 10