使用Java中的两个线程打印偶数和奇数

时间:2013-05-22 10:29:42

标签: java multithreading synchronization

我尝试了下面的代码。我从其他一些帖子中获取了这段代码,这些帖子根据作者是正确的。但是当我尝试跑步时,它并没有给我确切的结果。

这主要是为了按顺序打印偶数和奇数值。

public class PrintEvenOddTester {



    public static void main(String ... args){
        Printer print = new Printer(false);
        Thread t1 = new Thread(new TaskEvenOdd(print));
        Thread t2 = new Thread(new TaskEvenOdd(print));
        t1.start();
        t2.start();
    }


}



class TaskEvenOdd implements Runnable {

    int number=1;
    Printer print;

    TaskEvenOdd(Printer print){
        this.print = print;
    }

    @Override
    public void run() {

        System.out.println("Run method");
        while(number<10){

            if(number%2 == 0){
                System.out.println("Number is :"+ number);
                print.printEven(number);
                number+=2;
            }
            else {
                System.out.println("Number is :"+ number);
                print.printOdd(number);
                number+=2;
            }
        }

      }

    }

class Printer {

    boolean isOdd;

    Printer(boolean isOdd){
        this.isOdd = isOdd;
    }

    synchronized void printEven(int number) {

        while(isOdd){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Even:"+number);
        isOdd = true;
        notifyAll();
    }

    synchronized void printOdd(int number) {
        while(!isOdd){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Odd:"+number);
        isOdd = false;
        notifyAll();
    }

}

有人可以帮我解决这个问题吗?

修改 预期结果: 奇数:1 偶:2 奇数:3 偶:4 奇:5 偶:6 奇:7 即使是:8 奇:9

42 个答案:

答案 0 :(得分:48)

找到解决方案。寻找这个问题的解决方案的人可以参考: - )

public class PrintEvenOddTester {

    public static void main(String... args) {
        Printer print = new Printer();
        Thread t1 = new Thread(new TaskEvenOdd(print, 10, false));
        Thread t2 = new Thread(new TaskEvenOdd(print, 10, true));
        t1.start();
        t2.start();
    }

}

class TaskEvenOdd implements Runnable {

    private int max;
    private Printer print;
    private boolean isEvenNumber;

    TaskEvenOdd(Printer print, int max, boolean isEvenNumber) {
        this.print = print;
        this.max = max;
        this.isEvenNumber = isEvenNumber;
    }

    @Override
    public void run() {

        //System.out.println("Run method");
        int number = isEvenNumber == true ? 2 : 1;
        while (number <= max) {

            if (isEvenNumber) {
                //System.out.println("Even :"+ Thread.currentThread().getName());
                print.printEven(number);
                //number+=2;
            } else {
                //System.out.println("Odd :"+ Thread.currentThread().getName());
                print.printOdd(number);
                // number+=2;
            }
            number += 2;
        }

    }

}

class Printer {

    boolean isOdd = false;

    synchronized void printEven(int number) {

        while (isOdd == false) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Even:" + number);
        isOdd = false;
        notifyAll();
    }

    synchronized void printOdd(int number) {
        while (isOdd == true) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Odd:" + number);
        isOdd = true;
        notifyAll();
    }

}

这样可以输出:

Odd:1
Even:2
Odd:3
Even:4
Odd:5
Even:6
Odd:7
Even:8
Odd:9
Even:10

答案 1 :(得分:11)

以下是我通过单个类

开始工作的代码
package com.learn.thread;

public class PrintNumbers extends Thread {
volatile static int i = 1;
Object lock;

PrintNumbers(Object lock) {
    this.lock = lock;
}

public static void main(String ar[]) {
    Object obj = new Object();
    // This constructor is required for the identification of wait/notify
    // communication
    PrintNumbers odd = new PrintNumbers(obj);
    PrintNumbers even = new PrintNumbers(obj);
    odd.setName("Odd");
    even.setName("Even");
    odd.start();
    even.start();
}

@Override
public void run() {
    while (i <= 10) {
        if (i % 2 == 0 && Thread.currentThread().getName().equals("Even")) {
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + " - "
                        + i);
                i++;
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        if (i % 2 == 1 && Thread.currentThread().getName().equals("Odd")) {
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + " - "
                        + i);
                i++;
                lock.notify();
              }
           }
        }
    }
}

<强>输出:

Odd - 1
Even - 2
Odd - 3
Even - 4
Odd - 5
Even - 6
Odd - 7
Even - 8
Odd - 9
Even - 10
Odd - 11

答案 2 :(得分:8)

使用以下非常简单的JAVA 8 Runnable Class功能

public class MultiThreadExample {

static AtomicInteger atomicNumber = new AtomicInteger(1);

public static void main(String[] args) {
    Runnable print = () -> {
        while (atomicNumber.get() < 10) {
            synchronized (atomicNumber) {
                if ((atomicNumber.get() % 2 == 0) && "Even".equals(Thread.currentThread().getName())) {
                    System.out.println("Even" + ":" + atomicNumber.getAndIncrement());
                } else if ((atomicNumber.get() % 2 != 0) && "Odd".equals(Thread.currentThread().getName())) {
                    System.out.println("Odd" + ":" + atomicNumber.getAndIncrement());
                }
            }
        }
    };

    Thread t1 = new Thread(print);
    t1.setName("Even");
    t1.start();
    Thread t2 = new Thread(print);
    t2.setName("Odd");
    t2.start();

}
}

答案 3 :(得分:3)

   private Object lock = new Object();
   private volatile boolean isOdd = false;


    public void generateEvenNumbers(int number) throws InterruptedException {

        synchronized (lock) {
            while (isOdd == false) 
            {
                lock.wait();
            }
            System.out.println(number);
            isOdd = false;
            lock.notifyAll();
        }
    }

    public void generateOddNumbers(int number) throws InterruptedException {

        synchronized (lock) {
            while (isOdd == true) {
                lock.wait();
            }
            System.out.println(number);
            isOdd = true;
            lock.notifyAll();
        }
    }

答案 4 :(得分:2)

此代码也可以正常使用。

class Thread1 implements Runnable {

    private static boolean evenFlag = true;

    public synchronized void run() {
        if (evenFlag == true) {
            printEven();
        } else {
           printOdd();
        }
    }

    public void printEven() {
        for (int i = 0; i <= 10; i += 2) {
            System.out.println(i+""+Thread.currentThread());
        }
        evenFlag = false;
    }

    public  void printOdd() {
        for (int i = 1; i <= 11; i += 2) {
            System.out.println(i+""+Thread.currentThread());
        }
        evenFlag = true;
    }
}

public class OddEvenDemo {

    public static void main(String[] args) {

        Thread1 t1 = new Thread1();
        Thread td1 = new Thread(t1);
        Thread td2 = new Thread(t1);
        td1.start();
        td2.start();

    }
}

答案 5 :(得分:2)

使用Lock界面也可以这样做:

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class NumberPrinter implements Runnable {
    private Lock lock;
    private Condition condition;
    private String type;
    private static boolean oddTurn = true;

    public NumberPrinter(String type, Lock lock, Condition condition) {
        this.type = type;
        this.lock = lock;
        this.condition = condition;
    }

    public void run() {
        int i = type.equals("odd") ? 1 : 2;
        while (i <= 10) {
            if (type.equals("odd"))
                printOdd(i);
            if (type.equals("even"))
                printEven(i);
            i = i + 2;
        }
    }

    private void printOdd(int i) {
        // synchronized (lock) {
        lock.lock();
        while (!oddTurn) {
            try {
                // lock.wait();
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(type + " " + i);
        oddTurn = false;
        // lock.notifyAll();
        condition.signalAll();
        lock.unlock();
    }

    // }

    private void printEven(int i) {
        // synchronized (lock) {
        lock.lock();
        while (oddTurn) {
            try {
                // lock.wait();
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(type + " " + i);
        oddTurn = true;
        // lock.notifyAll();
        condition.signalAll();
        lock.unlock();
    }

    // }

    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        Thread odd = new Thread(new NumberPrinter("odd", lock, condition));
        Thread even = new Thread(new NumberPrinter("even", lock, condition));
        odd.start();
        even.start();
    }
}

答案 6 :(得分:2)

最简单的解决方案!

public class OddEvenWithThread {
    public static void main(String a[]) {
        Thread t1 = new Thread(new OddEvenRunnable(0), "Even Thread");
        Thread t2 = new Thread(new OddEvenRunnable(1), "Odd Thread");

        t1.start();
        t2.start();
    }
}

class OddEvenRunnable implements Runnable {
    Integer evenflag;
    static Integer number = 1;
    static Object lock = new Object();

    OddEvenRunnable(Integer evenFlag) {
        this.evenflag = evenFlag;
    }

    @Override
    public void run() {
        while (number < 10) {
            synchronized (lock) {
                try {
                    while (number % 2 != evenflag) {
                        lock.wait();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println(Thread.currentThread().getName() + " " + number);
                number++;
                lock.notifyAll();
            }
        }
    }
}

答案 7 :(得分:1)

Simpler Version in Java 8:

public class EvenOddPrinter {
    static boolean flag = true;
    public static void main(String[] args) {
        Runnable odd = () -> {
            for (int i = 1; i <= 10;) {
                if(EvenOddPrinter.flag) {
                    System.out.println(i);
                    i+=2;
                    EvenOddPrinter.flag = !EvenOddPrinter.flag;
                }
            }
        };

        Runnable even = () -> {
            for (int i = 2; i <= 10;) {
                if(!EvenOddPrinter.flag) {
                    System.out.println(i);
                    i+=2;
                    EvenOddPrinter.flag = !EvenOddPrinter.flag;
                }
            }
        };

         Thread t1 = new Thread(odd, "Odd");
         Thread t2 = new Thread(even, "Even");
         t1.start();
         t2.start();
    }
}

答案 8 :(得分:1)

另一个问题因此重复而被关闭。我认为我们可以安全摆脱“偶数或奇数”问题并使用wait/notify结构如下:

public class WaitNotifyDemoEvenOddThreads {
    /**
     * A transfer object, only use with proper client side locking!
     */
    static final class LastNumber {
        int num;
        final int limit;

        LastNumber(int num, int limit) {
            this.num = num;
            this.limit = limit;
        }
    }

    static final class NumberPrinter implements Runnable {
        private final LastNumber last;
        private final int init;

        NumberPrinter(LastNumber last, int init) {
            this.last = last;
            this.init = init;
        }

        @Override
        public void run() {
            int i = init;
            synchronized (last) {
                while (i <= last.limit) {
                    while (last.num != i) {
                        try {
                            last.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName() + " prints: " + i);
                    last.num = i + 1;
                    i += 2;
                    last.notify();
                }
            }
        }
    }

    public static void main(String[] args) {
        LastNumber last = new LastNumber(0, 10); // or 0, 1000
        NumberPrinter odd = new NumberPrinter(last, 1);
        NumberPrinter even = new NumberPrinter(last, 0);
        new Thread(odd, "o").start();
        new Thread(even, "e").start();
    }
}

答案 9 :(得分:1)

import java.util.concurrent.atomic.AtomicInteger;


public class PrintEvenOddTester {
      public static void main(String ... args){
            Printer print = new Printer(false);
            Thread t1 = new Thread(new TaskEvenOdd(print, "Thread1", new AtomicInteger(1)));
            Thread t2 = new Thread(new TaskEvenOdd(print,"Thread2" , new AtomicInteger(2)));
            t1.start();
            t2.start();
        }
}

class TaskEvenOdd implements Runnable {
    Printer print;
    String name;
    AtomicInteger number;
    TaskEvenOdd(Printer print, String name, AtomicInteger number){
        this.print = print;
        this.name = name;
        this.number = number;
    }

    @Override
    public void run() {

        System.out.println("Run method");
        while(number.get()<10){

            if(number.get()%2 == 0){
                print.printEven(number.get(),name);
            }
            else {
                print.printOdd(number.get(),name);
            }
            number.addAndGet(2);
        }

      }

    }



class Printer {
    boolean isEven;

    public Printer() {  }

    public Printer(boolean isEven) {
        this.isEven = isEven;
    }

    synchronized void printEven(int number, String name) {

        while (!isEven) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(name+": Even:" + number);
        isEven = false;
        notifyAll();
    }

    synchronized void printOdd(int number, String name) {
        while (isEven) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(name+": Odd:" + number);
        isEven = true;
        notifyAll();
    }
}

答案 10 :(得分:0)

package example;

public class PrintSeqTwoThreads {

    public static void main(String[] args) {
        final Object mutex = new Object();
        Thread t1 = new Thread() {
            @Override
            public void run() {
                for (int j = 0; j < 10;) {
                    synchronized (mutex) {
                        System.out.println(Thread.currentThread().getName() + " " + j);
                        j = j + 2;
                        mutex.notify();
                        try {
                            mutex.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };
        Thread t2 = new Thread() {
            @Override
            public void run() {
                for (int j = 1; j < 10;) {
                    synchronized (mutex) {
                        System.out.println(Thread.currentThread().getName() + " " + j);
                        j = j + 2;
                        mutex.notify();
                        try {
                            mutex.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };
        t1.start();
        t2.start();
    }
}

答案 11 :(得分:0)

请使用以下代码以正确的顺序打印奇数和偶数以及所需的消息。

package practice;


class Test {

  private static boolean oddFlag = true;
  int count = 1;

  private void oddPrinter() {
    synchronized (this) {
      while(true) {
        try {
          if(count < 10) {
            if(oddFlag) {
              Thread.sleep(500);
              System.out.println(Thread.currentThread().getName() + ": " + count++);
              oddFlag = !oddFlag;
              notifyAll();
            }
            else {
              wait();
            }
          }
          else {
            System.out.println("Odd Thread finished");
            notify();
            break;
          }
        }
        catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }

  private void evenPrinter() {
    synchronized (this) {
      while (true) {
        try {
          if(count < 10) {
            if(!oddFlag) {
              Thread.sleep(500);
              System.out.println(Thread.currentThread().getName() + ": " + count++);
              oddFlag = !oddFlag;
              notify();
            }
            else {
              wait();
            }
          }
          else {
            System.out.println("Even Thread finished");
            notify();
            break;
          }
        }
        catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }


  public static void main(String[] args) throws InterruptedException{
    final Test test = new Test();

    Thread t1 = new Thread(new Runnable() {
      public void run() {
        test.oddPrinter();
      }
    }, "Thread 1");

    Thread t2 = new Thread(new Runnable() {
      public void run() {
        test.evenPrinter();
      }
    }, "Thread 2");

    t1.start();
    t2.start();

    t1.join();
    t2.join();

    System.out.println("Main thread finished");
  }
}

答案 12 :(得分:0)

我无法理解这里的大多数代码所以我自己写了一个,也许它可以帮到像我这样的人:

注意:这不使用单独的打印偶数和奇数方法。一种方法print()可以做到这一切。

public class test {

    private static int START_INT = 1;
    private static int STOP_INT = 10;
    private static String THREAD_1 = "Thread A";
    private static String THREAD_2 = "Thread B";

    public static void main(String[] args) {
        SynchronizedRepository syncRep = new SynchronizedRepository(START_INT,STOP_INT);
        Runnable r1 = new EvenOddWorker(THREAD_1,syncRep);
        Runnable r2 = new EvenOddWorker(THREAD_2,syncRep);
        Thread t1 = new Thread(r1, THREAD_1);
        Thread t2 = new Thread(r2, THREAD_2);
        t1.start();
        t2.start();
    }

}




public class SynchronizedRepository {
    private volatile int number;
    private volatile boolean isSlotEven;
    private int startNumber;
    private int stopNumber;

    public SynchronizedRepository(int startNumber, int stopNumber) {
        super();
        this.number = startNumber;
        this.isSlotEven = startNumber%2==0;
        this.startNumber = startNumber;
        this.stopNumber = stopNumber;
    }


    public synchronized void print(String threadName) {
        try {
            for(int i=startNumber; i<=stopNumber/2; i++){
                if ((isSlotEven && number % 2 == 0)||
                        (!isSlotEven && number % 2 != 0)){
                    System.out.println(threadName + " "+ number);
                    isSlotEven = !isSlotEven;
                    number++;
                }
                notifyAll();
                wait();
            }
            notifyAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}



public class EvenOddWorker implements Runnable {

    private String threadName;
    private SynchronizedRepository syncRep;

    public EvenOddWorker(String threadName, SynchronizedRepository syncRep) {
        super();
        this.threadName = threadName;
        this.syncRep = syncRep;
    }

    @Override
    public void run() {
        syncRep.print(threadName);
    }

}

答案 13 :(得分:0)

简单解决方案:)

package com.code.threads;

public class PrintOddEven extends Thread {

    private Object lock;
    static volatile int count = 1;

    PrintOddEven(Object lock) {
        this.lock = lock;
    }

    @Override
    public void run () {
        while(count <= 10) {
            if (count % 2 == 0) {
                synchronized(lock){
                    System.out.println("Even - " + count);
                    ++count;
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                synchronized(lock){
                    System.out.println("Odd - " + count);
                    ++count;
                    lock.notify();
                }
            }
        }
    }

    public static void main(String[] args) {
        Object obj = new Object();
        PrintOddEven even = new PrintOddEven(obj);
        PrintOddEven odd = new PrintOddEven(obj);

        even.start();
        odd.start();
    }
}

答案 14 :(得分:0)

package programs.multithreading;

public class PrintOddEvenNoInSequence {

final int upto;
final PrintOddEvenNoInSequence obj;
volatile boolean oddFlag,evenFlag;
public PrintOddEvenNoInSequence(int upto){
    this.upto = upto;
    obj = this;
    oddFlag = true;
    evenFlag = false;
}
void printInSequence(){

    Thread odd = new Thread(new Runnable() {
        @Override
        public void run() {
            for(int i = 1; i <= upto; i = i + 2){
                synchronized (obj) {
                    while(!oddFlag){
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    System.out.println("Odd:"+i);
                    oddFlag = false;
                    evenFlag = true;
                    obj.notify();
                }
            }
        }
    });

    Thread even = new Thread(new Runnable() {
        @Override
        public void run() {
            for(int i = 2; i <= upto; i = i + 2){
                synchronized (obj) {
                    while(!evenFlag){
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    System.out.println("Even:"+i);
                    oddFlag = true;
                    evenFlag = false;
                    obj.notify();
                }
            }
        }
    });

    odd.start();
    even.start();

}
public static void main(String[] args) {
    new PrintOddEvenNoInSequence(100).printInSequence();
}
}

答案 15 :(得分:0)

下面的解决方案是使用 java 8 completable future and executor service 使用两个线程打印偶数和奇数。

ExecutorService firstExecutorService = Executors.newSingleThreadExecutor(r -> {
        Thread t = new Thread(r);
        t.setName("first");
        return t;
    });

    ExecutorService secondExecutorService = Executors.newSingleThreadExecutor(r -> {
        Thread t = new Thread(r);
        t.setName("second");
        return t;
    });

    IntStream.range(1, 101).forEach(num -> {

        CompletableFuture<Integer> thenApplyAsync = CompletableFuture.completedFuture(num).thenApplyAsync(x -> {

            if (x % 2 == 1) {
                System.out.println(x + " " + Thread.currentThread().getName());
            }
            return num;
        }, firstExecutorService);

        thenApplyAsync.join();

        CompletableFuture<Integer> thenApplyAsync2 = CompletableFuture.completedFuture(num).thenApplyAsync(x -> {
            if (x % 2 == 0) {
                System.out.println(x + " " + Thread.currentThread().getName());
            }
            return num;
        }, secondExecutorService);

        thenApplyAsync2.join();
    });

    firstExecutorService.shutdown();
    secondExecutorService.shutdown();

下面是它的控制台日志。
enter image description here

答案 16 :(得分:0)

        public class OddAndEvenThreadProblems {
            private static Integer i = 0;

            public static void main(String[] args) {
                new EvenClass().start();
                new OddClass().start();

            }

            public static class EvenClass extends Thread {

                public void run() {
                    while (i < 10) {
                        synchronized (i) {
                            if (i % 2 == 0 ) {
                                try {
                                    Thread.sleep(1000);
                                    System.out.println(" EvenClass " + i);
                                    i = i + 1;
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        }
                    }
                }
            }

            public static class OddClass extends Thread {

                @Override
                public void run() {
                    while (i < 10) {
                        synchronized (i) {
                            if (i % 2 == 1) {
                                try {
                                    Thread.sleep(1000);
                                    System.out.println(" OddClass  " + i);
                                    i = i + 1;
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                    }
                }
            }
        }
    }





OUTPUT will be :- 

 EvenClass 0
 OddClass  1
 EvenClass 2
 OddClass  3
 EvenClass 4
 OddClass  5
 EvenClass 6
 OddClass  7
 EvenClass 8
 OddClass  9

答案 17 :(得分:0)

public class Main {
    public static void main(String[] args) throws Exception{
        int N = 100;
        PrintingThread oddNumberThread = new PrintingThread(N - 1);
        PrintingThread evenNumberThread = new PrintingThread(N);
        oddNumberThread.start();
        // make sure that even thread only start after odd thread
        while (!evenNumberThread.isAlive()) {
            if(oddNumberThread.isAlive()) {
                evenNumberThread.start();
            } else {
                Thread.sleep(100);
            }
        }

    }
}

class PrintingThread extends Thread {
    private static final Object object = new Object(); // lock for both threads
    final int N;
    // N determines whether given thread is even or odd
    PrintingThread(int N) {
        this.N = N;
    }

    @Override
    public void run() {
        synchronized (object) {
            int start = N % 2 == 0 ? 2 : 1; // if N is odd start from 1 else start from 0
            for (int i = start; i <= N; i = i + 2) {
                System.out.println(i);
                try {
                    object.notify(); // will notify waiting thread
                    object.wait(); // will make current thread wait
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

答案 18 :(得分:0)

请参阅Clean实施

TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
at stringify (C:\rater\rater\server\node_modules\express\lib\response.js:1119:12)
at ServerResponse.json (C:\rater\rater\server\node_modules\express\lib\response.js:260:14)
at ServerResponse.send (C:\rater\rater\server\node_modules\express\lib\response.js:158:21)
at app.get (C:\rater\rater\server\src\app.js:265:17)
at Layer.handle [as handle_request] (C:\rater\rater\server\node_modules\express\lib\router\layer.js:95:5)
at next (C:\rater\rater\server\node_modules\express\lib\router\route.js:137:13)
at complete (C:\rater\rater\server\node_modules\passport\lib\middleware\authenticate.js:263:13)
at C:\rater\rater\server\node_modules\passport\lib\middleware\authenticate.js:270:15
at pass (C:\rater\rater\server\node_modules\passport\lib\authenticator.js:431:14)
at Authenticator.transformAuthInfo (C:\rater\rater\server\node_modules\passport\lib\authenticator.js:453:5)
at C:\rater\rater\server\node_modules\passport\lib\middleware\authenticate.js:267:22
at C:\rater\rater\server\node_modules\passport\lib\http\request.js:52:7
at C:\rater\rater\server\node_modules\passport\lib\sessionmanager.js:26:5
at pass (C:\rater\rater\server\node_modules\passport\lib\authenticator.js:277:43)
at serialized (C:\rater\rater\server\node_modules\passport\lib\authenticator.js:286:7)

答案 19 :(得分:0)

这是解决此问题的最简单方法。  公共类OddEven实现了Runnable {

    @Override
    public void run() {
        // TODO Auto-generated method stub

        for (int i = 1; i <= 10; i++) {
            synchronized (this) {
                if (i % 2 == 0 && Thread.currentThread().getName().equals("t2")) {
                    try {
                        notifyAll();
                        System.out.println("Even Thread : " + i);
                        wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else if (i % 2 != 0
                        && Thread.currentThread().getName().equals("t1")) {
                    try {
                        notifyAll();
                        System.out.println("Odd Thread : " + i);
                        wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }

    }

    public static void main(String[] args) {

        OddEven obj = new OddEven();
        Thread t1 = new Thread(obj, "t1");
        Thread t2 = new Thread(obj, "t2");
        t1.start();
        t2.start();

    }
}

答案 20 :(得分:0)

public class OddEven implements Runnable {
    public  int count = 0;

    @Override
    public void run() {
        try {
            increment();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    synchronized void increment() throws Exception {
        while(true) {
            if(count<10) {
                count++;
                System.out.println(Thread.currentThread().getName()+"  " +count);
                notify();
                wait();
            }else {
                break;
            }
        }
    }

    public static void main(String[] args) {
        OddEven n1 = new OddEven();
        Thread r1 = new Thread(n1);
        Thread r2 = new Thread(n1);

        r1.start();
        r2.start();

    }

}

答案 21 :(得分:0)

1-用1初始化数字,并将isOdd标志设置为 false 。将isOdd设置为 true

2-增量应为1(而不是2),即number+=1;

答案 22 :(得分:0)

class PrintNumberTask implements Runnable {
Integer count;
Object lock;

PrintNumberTask(int i, Object object) {
    this.count = i;
    this.lock = object;
}

@Override
public void run() {
    while (count <= 10) {
        synchronized (lock) {
            if (count % 2 == 0) {
                System.out.println(count);
                count++;
                lock.notify();
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println(count);
                count++;
                lock.notify();
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

}

答案 23 :(得分:0)

此解决方案适用于我(Java 8 及更高版本使用 Lamda 表达式)

public class EvenOddUsingThread {

    static int number = 100;
    static int counter = 1;

    public static void main(String[] args) {
        EvenOddUsingThread eod = new EvenOddUsingThread();
        Thread t1 = new Thread(() -> eod.printOdd());
        Thread t2 = new Thread(() -> eod.printEven());
        t1.start();
        t2.start();
    }

    public void printOdd() {
        synchronized (this) {
            while (counter < number) {
                if (counter % 2 == 0) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                    }
                }
                // Print the number
                System.out.println(counter + " Thread Name: " + Thread.currentThread().getName());
                // Increment counter
                counter++;
                // Notify to second thread
                notify();
            }
        }
    }

    public void printEven() {
        synchronized (this) {
            while (counter < number) {
                if (counter % 2 == 1) {
                    try {
                        wait();
                    } catch (InterruptedException ignored) {
                    }
                }
                // Print the number
                System.out.println(counter + " Thread Name: " + Thread.currentThread().getName());
                // Increment counter
                counter++;
                // Notify to second thread
                notify();
            }
        }
    }

}

答案 24 :(得分:0)

公共类解决方案{

 static class NumberGenerator{

     private static volatile boolean printEvenNumber = false;


     public  void printEvenNumber(int i) {
         synchronized (this) {
             if(!printEvenNumber) {
                 try {
                     wait();
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
             }
             System.out.println(i);
             printEvenNumber = !printEvenNumber;
             notify();
         }
     }

     public  void printOddNumber(int i ) {
            synchronized (this) {
                if(printEvenNumber) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                System.out.println(i);
                printEvenNumber = !printEvenNumber;
                notify();
            }
     }

}

static  class OddNumberGenerator implements Runnable{
    private NumberGenerator numberGenerator;

    public OddNumberGenerator(NumberGenerator numberGenerator) {
        this.numberGenerator = numberGenerator;
    }

    @Override
    public void run() {
        for(int i  = 1; i <100; i = i + 2) {
            numberGenerator.printOddNumber(i);
        }
    }
}

static class EvenNumberGenerator implements Runnable {
    private NumberGenerator numberGenerator;

    public EvenNumberGenerator(NumberGenerator numberGenerator) {
        this.numberGenerator = numberGenerator;
    }

    @Override
    public void run() {
        for (int i = 2; i <= 100; i =  i + 2) {
           numberGenerator.printEvenNumber(i);
        }
    }
}


public static void main(String[] args) {
    NumberGenerator ng = new NumberGenerator();
    OddNumberGenerator oddNumberGenerator = new OddNumberGenerator(ng);
    EvenNumberGenerator evenNumberGenerator = new EvenNumberGenerator(ng);
    new Thread(oddNumberGenerator).start();
    new Thread(evenNumberGenerator).start();

}

}

答案 25 :(得分:0)

打印奇数偶数的类

public class PrintOddEven implements Runnable {

    private int max;
    private int number;

    public  PrintOddEven(int max_number,int number) {
        max = max_number;
        this.number = number;
    }

    @Override
    public void run() {


        while(number<=max)
        {
            if(Thread.currentThread().getName().equalsIgnoreCase("odd"))
            {
                try {
                    printOdd();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            else
            {
                try {
                    printEven();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }


    }

    public synchronized void printOdd() throws InterruptedException
    {

        if(number%2==0)
        {
            wait();
        }

        System.out.println(number+Thread.currentThread().getName());
        number++;
        notifyAll();
    }

    public synchronized void printEven() throws InterruptedException
    {

        if(number%2!=0)
        {
            wait();
        }

        System.out.println(number+Thread.currentThread().getName());
        number++;
        notifyAll();
    }

}

驱动程序

public class OddEvenThread {

    public static void main(String[] args) {

        PrintOddEven printer = new PrintOddEven(10,1);  
        Thread thread1 = new Thread(printer,"odd");
        Thread thread2 = new Thread (printer,"even");

        thread1.start();
        thread2.start();

    }

}

答案 26 :(得分:0)

这可以使用Lock and Condition:

来实现
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class EvenOddThreads {

    public static void main(String[] args) throws InterruptedException {
        Printer p = new Printer();
        Thread oddThread = new Thread(new PrintThread(p,false),"Odd  :");
        Thread evenThread = new Thread(new PrintThread(p,true),"Even :");
        oddThread.start();
        evenThread.start();
    }

}

class PrintThread implements Runnable{
    Printer p;
    boolean isEven = false;

    PrintThread(Printer p, boolean isEven){
        this.p = p;
        this.isEven = isEven;
    }

    @Override
    public void run() {
        int i = (isEven==true) ? 2 : 1;
        while(i < 10 ){
            if(isEven){
                p.printEven(i);
            }else{
                p.printOdd(i);
            }
            i=i+2;
        }
    }
}

class Printer{

    boolean isEven = true;
    Lock lock = new ReentrantLock();
    Condition condEven = lock.newCondition();
    Condition condOdd = lock.newCondition();

    public void printEven(int no){
        lock.lock();
        while(isEven==true){
            try {
                condEven.await();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName() +no);
        isEven = true;
        condOdd.signalAll();
        lock.unlock();
    }

    public void printOdd(int no){
        lock.lock();
        while(isEven==false){
            try {
                condOdd.await();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName() +no);
        isEven = false;
        condEven.signalAll();
        lock.unlock();
    }
}

答案 27 :(得分:0)

您可以使用以下代码通过创建两个匿名线程类来获取输出。

package practice;

class Display {
    boolean isEven = false;

    synchronized public void printEven(int number) throws InterruptedException {
        while (isEven)
            wait();
        System.out.println("Even : " + number);
        isEven = true;
        notify();
    }

    synchronized public void printOdd(int number) throws InterruptedException {
        while (!isEven)
            wait();
        System.out.println("Odd : " + number);
        isEven = false;
        notify();
    }
}

public class OddEven {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        final Display disp = new Display();


        new Thread() {
            public void run() {
                int num = 0;
                for (int i = num; i <= 10; i += 2) {
                    try {
                        disp.printEven(i);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread() {
            public void run() {
                int num = 1;
                for (int i = num; i <= 10; i += 2) {
                    try {
                        disp.printOdd(i);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

}

答案 28 :(得分:0)

使用单一班级的工作解决方案

package com.fursa.threads;

   public class PrintNumbers extends Thread {

     Object lock;

    PrintNumbers(Object lock) {
         this.lock = lock;
    }

    public static void main(String ar[]) {
        Object obj = new Object();
        // This constructor is required for the identification of wait/notify
        // communication
        PrintNumbers odd = new PrintNumbers(obj);
        PrintNumbers even = new PrintNumbers(obj);
        odd.setName("Odd");
        even.setName("Even");
        even.start();
        odd.start();

    }

    @Override
    public void run() {
        for(int i=0;i<=100;i++) {

            synchronized (lock) {

                if (Thread.currentThread().getName().equals("Even")) {

                    if(i % 2 == 0 ){
                        System.out.println(Thread.currentThread().getName() + " - "+ i);
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }                   
                    else if (i % 2 != 0 ) {
                        lock.notify();
                    }
                }

                if (Thread.currentThread().getName().equals("Odd")) {

                    if(i % 2 == 1 ){
                        System.out.println(Thread.currentThread().getName() + " - "+ i);
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }                   
                    else if (i % 2 != 1 ) {
                        lock.notify();
                    }
                }

            }
        }
    }
}

答案 29 :(得分:0)

package com.example;

public class MyClass  {
    static int mycount=0;
    static Thread t;
    static Thread t2;
    public static void main(String[] arg)
    {
        t2=new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.print(mycount++ + " even \n");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(mycount>25)
                    System.exit(0);
                run();
            }
        });
        t=new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.print(mycount++ + " odd \n");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(mycount>26)
                    System.exit(0);
                run();
            }
        });
        t.start();
        t2.start();
    }
}

答案 30 :(得分:0)

我认为提供的解决方案不必要地添加了东西,并且没有充分利用信号量。这就是我的解决方案。

[{"area_id":"1","area_name":"DLF Phase 1"},{"area_id":"2","area_name":"DLF Phase 2"}]

答案 31 :(得分:0)

public class ThreadEvenOdd {
  static int cnt=0;
  public static void main(String[] args) {

    Thread t1 = new Thread(new Runnable() {

      @Override
      public void run() {
        synchronized(this) {
          while(cnt<101) {
            if(cnt%2==0) {
              System.out.print(cnt+" ");
              cnt++;
            }
            notifyAll();
          }
        }
      }

    });
    Thread t2 = new Thread(new Runnable() {

      @Override
      public void run() {
        synchronized(this) {
          while(cnt<101) {
            if(cnt%2==1) {
              System.out.print(cnt+" ");
              cnt++;
            }
            notifyAll();
          }
        }
      }
    });

    t1.start();
    t2.start();
  }
}

答案 32 :(得分:0)

package pkgscjp;

public class OddPrint implements Runnable {

    public static boolean flag = true;

    public void run() {
        for (int i = 1; i <= 99;) {
            if (flag) {
                System.out.println(i);
                flag = false;
                i = i + 2;
            }
        }
    }

}


package pkgscjp;

public class EvenPrint implements Runnable {
    public void run() {
        for (int i = 2; i <= 100;) {
            if (!OddPrint.flag) {
                System.out.println(i);
                OddPrint.flag = true;
                i = i + 2;
            }
        }

    }
}


package pkgscjp;

public class NaturalNumberThreadMain {
    public static void main(String args[]) {
        EvenPrint ep = new EvenPrint();
        OddPrint op = new OddPrint();
        Thread te = new Thread(ep);
        Thread to = new Thread(op);
        to.start();
        te.start();

    }

}

答案 33 :(得分:0)

以下是我使用2个信号量的实现。

  1. 奇数信号量,持有许可证1。
  2. 即使信号量持有许可证0。
  3. 将两个信号量传递给两个线程,如下所示(我的,其他): -
  4. To Odd thread pass in order(odd,even)
  5. To even thread以此顺序传递(偶数,奇数)
  6. run()方法逻辑是my.acquireUninterruptibly() - &gt;打印 - &gt; other.release()
  7. 在偶数线程中,即使Sema为0,它也会阻塞。
  8. 在Odd线程中,由于奇数Sema可用(init为1),这将打印1,然后释放甚至Sema,允许Even线程运行。
  9. 甚至线程运行打印2并释放奇数Sema,允许奇数线程运行。

    import java.util.concurrent.Semaphore;
    
    
    public class EvenOdd {
    private final static String ODD = "ODD";
    private final static String EVEN = "EVEN";
    private final static int MAX_ITERATIONS = 10;
    
    public static class EvenOddThread implements Runnable {     
        private String mType;
        private int  mNum;
        private Semaphore mMySema;
        private Semaphore mOtherSema;
    
        public EvenOddThread(String str, Semaphore mine, Semaphore other) {
            mType = str;
            mMySema = mine;//new Semaphore(1); // start out as unlocked
            mOtherSema = other;//new Semaphore(0);
            if(str.equals(ODD)) {
                mNum = 1;
            }
            else {
                mNum = 2;
            }
        }
    
        @Override
        public void run() {         
    
                for (int i = 0; i < MAX_ITERATIONS; i++) {
                    mMySema.acquireUninterruptibly();
                    if (mType.equals(ODD)) {
                        System.out.println("Odd Thread - " + mNum);
                    } else {
                        System.out.println("Even Thread - " + mNum);
                    }
                    mNum += 2;
                    mOtherSema.release();
                }           
        }
    
    }
    
        public static void main(String[] args) throws InterruptedException {
            Semaphore odd = new Semaphore(1);
            Semaphore even = new Semaphore(0);
    
            System.out.println("Start!!!");
            System.out.println();
    
            Thread tOdd = new Thread(new EvenOddThread(ODD, 
                                     odd, 
                                     even));
            Thread tEven = new Thread(new EvenOddThread(EVEN, 
                                     even, 
                                     odd));
    
            tOdd.start();
            tEven.start();
    
            tOdd.join();
            tEven.join();
    
            System.out.println();
            System.out.println("Done!!!");
        }       
    
    }
    
  10. 以下是输出: -

    Start!!!
    
    Odd Thread - 1
    Even Thread - 2
    Odd Thread - 3
    Even Thread - 4
    Odd Thread - 5
    Even Thread - 6
    Odd Thread - 7
    Even Thread - 8
    Odd Thread - 9
    Even Thread - 10
    Odd Thread - 11
    Even Thread - 12
    Odd Thread - 13
    Even Thread - 14
    Odd Thread - 15
    Even Thread - 16
    Odd Thread - 17
    Even Thread - 18
    Odd Thread - 19
    Even Thread - 20
    
    Done!!!
    

答案 34 :(得分:0)

这是使用等待和通知机制打印奇数甚至没有的工作代码。我限制数字的限制打印1到50。

public class NotifyTest {
    Object ob=new Object(); 

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    NotifyTest nt=new NotifyTest();

    even e=new even(nt.ob);     
    odd o=new odd(nt.ob);

    Thread t1=new Thread(e);
    Thread t2=new Thread(o);

    t1.start();     
    t2.start();
    }
}    

class even implements Runnable
{
    Object lock;        
    int i=2;

    public even(Object ob)
    {
        this.lock=ob;       
    }

    @Override
    public void run() {
    // TODO Auto-generated method stub      
        while(i<=50)
        {
            synchronized (lock) {               
            try {
                lock.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.println("Even Thread Name-->>" + Thread.currentThread().getName() + "Value-->>" + i);
            i=i+2;              
        }           
    }       
} 

class odd implements Runnable
{

    Object lock;
    int i=1;    

    public odd(Object ob)
    {
        this.lock=ob;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(i<=49)
        {
            synchronized (lock) {               
            System.out.println("Odd Thread Name-->>" + Thread.currentThread().getName() + "Value-->>" + i);
            i=i+2;              
            lock.notify();
            }
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }       
}

答案 35 :(得分:0)

这是我解决问题的方法。我有两个实现Runnable的类,一个打印奇数序列,另一个打印偶数。我有一个Object的实例,我用它来锁定。我使用相同的对象初始化两个类。在两个类的run方法中有一个synchronized block,其中,在循环内,每个方法打印一个数字,通知另一个线程,等待锁定同一个对象然后自己等待相同的再次锁定。

课程:

public class PrintEven implements Runnable{
private Object lock;
public PrintEven(Object lock) {
    this.lock =  lock;
}
@Override
public void run() {
    synchronized (lock) {
        for (int i = 2; i <= 10; i+=2) {
            System.out.println("EVEN:="+i);
            lock.notify();
            try {
                //if(i!=10) lock.wait();
                lock.wait(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

  }
}


public class PrintOdd implements Runnable {
private Object lock;
public PrintOdd(Object lock) {
    this.lock =  lock;
}
@Override
public void run() {
    synchronized (lock) {
        for (int i = 1; i <= 10; i+=2) {
            System.out.println("ODD:="+i);
            lock.notify();
            try {
                //if(i!=9) lock.wait();
                lock.wait(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
}

public class PrintEvenOdd {
public static void main(String[] args){
    Object lock = new Object(); 
    Thread thread1 =  new Thread(new PrintOdd(lock));
    Thread thread2 =  new Thread(new PrintEven(lock));
    thread1.start();
    thread2.start();
}
}

我的例子中的上限是10.一旦奇数线程打印9或偶数线程打印10,那么我们不再需要任何线程等待。因此,我们可以使用一个if-block来处理这个问题。或者,我们可以使用重载的wait(long timeout)方法来等待超时。 但有一个漏洞。使用此代码,我们无法保证哪个线程将首先开始执行。

另一个例子,使用锁定和条件

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockConditionOddEven {
 public static void main(String[] args) {
    Lock lock =  new ReentrantLock();
    Condition evenCondition = lock.newCondition();
    Condition oddCondition = lock.newCondition();
    Thread evenThread =  new Thread(new EvenPrinter(10, lock, evenCondition, oddCondition));
    Thread oddThread =  new Thread(new OddPrinter(10, lock, evenCondition, oddCondition));
    oddThread.start();
    evenThread.start();
}

static class OddPrinter implements Runnable{
    int i = 1;
    int limit;
    Lock lock;
    Condition evenCondition;
    Condition oddCondition;

    public OddPrinter(int limit) {
        super();
        this.limit = limit;
    }

    public OddPrinter(int limit, Lock lock, Condition evenCondition, Condition oddCondition) {
        super();
        this.limit = limit;
        this.lock = lock;
        this.evenCondition = evenCondition;
        this.oddCondition = oddCondition;
    }

    @Override
    public void run() {
        while( i <=limit) {
            lock.lock();
            System.out.println("Odd:"+i);
            evenCondition.signal();
            i+=2;
            try {
                oddCondition.await();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
    }
}

static class EvenPrinter implements Runnable{
    int i = 2;
    int limit;
    Lock lock;
    Condition evenCondition;
    Condition oddCondition;

    public EvenPrinter(int limit) {
        super();
        this.limit = limit;
    }


    public EvenPrinter(int limit, Lock lock, Condition evenCondition, Condition oddCondition) {
        super();
        this.limit = limit;
        this.lock = lock;
        this.evenCondition = evenCondition;
        this.oddCondition = oddCondition;
    }


    @Override
    public void run() {
        while( i <=limit) {
            lock.lock();
            System.out.println("Even:"+i);
            i+=2;
            oddCondition.signal();
            try {
                evenCondition.await();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
    }
}

}

答案 36 :(得分:0)

我这样做了,使用两个线程打印时我们无法预测哪个线程的顺序    将首先执行,以便克服这种情况,我们必须同步共享资源,在    我的情况是两个线程试图访问的打印功能。

class Printoddeven{

    public synchronized void print(String msg) {
        try {
            if(msg.equals("Even")) {
                for(int i=0;i<=10;i+=2) {
                    System.out.println(msg+" "+i);
                    Thread.sleep(2000);
                    notify();
                    wait();
                }
            } else {
                for(int i=1;i<=10;i+=2) {
                    System.out.println(msg+" "+i);
                    Thread.sleep(2000);
                    notify();
                    wait();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

class PrintOdd extends Thread{
    Printoddeven oddeven;
    public PrintOdd(Printoddeven oddeven){
        this.oddeven=oddeven;
    }

    public void run(){
        oddeven.print("ODD");
    }
}

class PrintEven extends Thread{
    Printoddeven oddeven;
    public PrintEven(Printoddeven oddeven){
        this.oddeven=oddeven;
    }

    public void run(){
        oddeven.print("Even");
    }
}



public class mainclass 
{
    public static void main(String[] args) {
        Printoddeven obj = new Printoddeven();//only one object  
        PrintEven t1=new PrintEven(obj);  
        PrintOdd t2=new PrintOdd(obj);  
        t1.start();  
        t2.start();  
    }
}

答案 37 :(得分:-1)

公共类EvenOddex {

public static class print {

    int n;
    boolean isOdd = false;

    synchronized public void printEven(int n) {

        while (isOdd) {
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(EvenOddex.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        System.out.print(Thread.currentThread().getName() + n + "\n");

        isOdd = true;
        notify();
    }

    synchronized public void printOdd(int n) {
        while (!isOdd) {
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(EvenOddex.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


        System.out.print(Thread.currentThread().getName() + n + "\n");
        isOdd = false;
        notify();



    }
}

public static class even extends Thread {

    print po;

    even(print po) {

        this.po = po;

        new Thread(this, "Even").start();

    }

    @Override
    public void run() {


        for (int j = 0; j < 10; j++) {
            if ((j % 2) == 0) {
                po.printEven(j);
            }
        }

    }
}

public static class odd extends Thread {

    print po;

    odd(print po) {

        this.po = po;
        new Thread(this, "Odd").start();
    }

    @Override
    public void run() {

        for (int i = 0; i < 10; i++) {

            if ((i % 2) != 0) {
                po.printOdd(i);
            }
        }

    }
}

public static void main(String args[]) {
    print po = new print();
    new even(po);
    new odd(po);

}

}

答案 38 :(得分:-1)

public class Multi extends Thread{  
    public static int a;
    static{a=1;}
    public void run(){  
        for(int i=1;i<5;i++){  
        System.out.println("Thread Id  "+this.getId()+"  Value "+a++);
        try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}  

        }  
    }  
public static void main(String args[]){  
       Multi t1=new Multi();  
       Multi t2=new Multi();  

      t1.start();  
      t2.start();  
    }  
}  

答案 39 :(得分:-1)

public class PrintOddEven {
private static class PrinterThread extends Thread {

    private static int current = 0;
    private static final Object LOCK = new Object();

    private PrinterThread(String name, int number) {
        this.name = name;
        this.number = number;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (LOCK) {
                try {
                    LOCK.wait(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if (current < number) {
                    System.out.println(name + ++current);
                } else {
                    break;
                }

                LOCK.notifyAll();
            }
        }
    }

    int number;
    String name;
}

public static void main(String[] args) {
    new PrinterThread("thread1 : ", 20).start();
    new PrinterThread("thread2 : ", 20).start();
}
}

答案 40 :(得分:-1)

以下简单解决方案: -

1000

答案 41 :(得分:-2)

import java.util.concurrent.Semaphore;


public class PrintOddAndEven {

private static class OddThread extends Thread {
    private Semaphore semaphore;
    private Semaphore otherSemaphore;
    private int value = 1;

    public  OddThread(Semaphore semaphore, Semaphore otherSemaphore) {
        this.semaphore = semaphore;
        this.otherSemaphore = otherSemaphore;
    }

    public void run() {
        while (value <= 100) {
            try {
                // Acquire odd semaphore
                semaphore.acquire();
                System.out.println(" Odd Thread " + value + " " + Thread.currentThread().getName());

            } catch (InterruptedException excetion) {
                excetion.printStackTrace();
            }
            value = value + 2;
            // Release odd semaphore
            otherSemaphore.release();
        }
    }
}


private static class EvenThread extends Thread {
    private Semaphore semaphore;
    private Semaphore otherSemaphore;

    private int value = 2;

    public  EvenThread(Semaphore semaphore, Semaphore otherSemaphore) {
        this.semaphore = semaphore;
        this.otherSemaphore = otherSemaphore;
    }

    public void run() {
        while (value <= 100) {
            try {
                // Acquire even semaphore
                semaphore.acquire();
                System.out.println(" Even Thread " + value + " " + Thread.currentThread().getName());

            } catch (InterruptedException excetion) {
                excetion.printStackTrace();
            }
            value = value + 2;
            // Release odd semaphore
            otherSemaphore.release();
        }
    }
}


public static void main(String[] args) {
    //Initialize oddSemaphore with permit 1
    Semaphore oddSemaphore = new Semaphore(1);
    //Initialize evenSempahore with permit 0
    Semaphore evenSempahore = new Semaphore(0);
    OddThread oddThread = new OddThread(oddSemaphore, evenSempahore);
    EvenThread evenThread = new EvenThread(evenSempahore, oddSemaphore);
    oddThread.start();
    evenThread.start();
    }
}