问题描述
]public synchronized void run(){oushu();qishu();}public void oushu(){ //偶数for (int i = 0; i < 101; i++) {if(i%2==0){System.out.println(Thread.currentThread().getName()+i);}}}public void qishu(){ //奇数for (int i = 0; i < 101; i++) {if(i%2!=0){引用
解决方案
例如 产生的奇数和偶数: 1,2,3,4 这样? 如果是这样应该是不行的 那个线程片段性的执行,就是有段时间是线程ou执行 有段时间是线程qi执行 但是执行的时候貌似是不能控制每个线程执行循环的次数 所以2个线程排序有点困难的
解决方案二:
public class Test15 implements Runnable{ int start=1;public Test15(int start){this.start=start;}@Overridepublic void run() {StringBuffer result= new StringBuffer(start+"");for(int i=0;i<49;i++){result.append(",");result.append(start+(i+1)*2);}System.out.println(result.toString());}public static void main(String[] args){new Thread(new Test15(1)).start();new Thread(new Test15(2)).start();}}
解决方案三:
呵呵,楼主这两个循环都可以把步长调整为每次+2,这样可以把里面判断为奇数还是偶数的代码省了。 这样,第一段执行完了之后,再让第二个线程开始执行,这个过程可以使用CountDownLatch来控制。或者使用阻塞队列,所有这些数据放到队列中,每个线程消费一个之后wait,同时notify让另一个线程来执行,这样达到分别输出的目的。
解决方案四:
可以用个线程池里面丢两个线程跑的如下:import java.util.concurrent.atomic.AtomicInteger;public class OddNumber implements Runnable{ private int maxNumber; private AtomicInteger number; private Object lock; public OddNumber(int maxNumber, AtomicInteger number, Object lock) { this.maxNumber = maxNumber; this.number = number; this.lock = lock; } public void run() { print(); } public void print() { while (number.get() < maxNumber + 1) { if (number.get() % 2 == 0) { System.out.println(Thread.currentThread().getName() + " --> " + number.getAndAdd(1)); synchronized (lock) { lock.notifyAll(); } } else { synchronized (lock) { try { lock.wait(); //wait for the lock } catch (InterruptedException e) { e.printStackTrace(); } } } } } }import java.util.concurrent.atomic.AtomicInteger;public class EvenNumber implements Runnable{ private int maxNumber; private AtomicInteger number; private Object lock; public EvenNumber(int maxNumber, AtomicInteger number, Object lock) { this.maxNumber = maxNumber; this.number = number; this.lock = lock; } public void run() { print(); } public void print() { while (number.get() < maxNumber + 1) { if (number.get() % 2 != 0) { System.out.println(Thread.currentThread().getName() + " --> " + number.getAndAdd(1)); synchronized (lock) { lock.notify(); // } } else { synchronized (lock) { try { lock.wait(); //wait for the lock } catch (InterruptedException e) { e.printStackTrace(); } } } } } }package com.mycompany.app6;import java.util.concurrent.Executor;import java.util.concurrent.Executors;import java.util.concurrent.atomic.AtomicInteger;public class PrintOddEvenNumbersWithTwoThreads{ public static void main(String[] args) { final int max = 10; final AtomicInteger i = new AtomicInteger(1); //start with 0 Executor dd = Executors.newFixedThreadPool(2); final Object lock = new Object(); dd.execute(new OddNumber(max, i, lock)); dd.execute(new EvenNumber(max, i, lock)); do { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } while (i.get() != max + 1); System.out.println("nDone"); System.exit(0); }}