diff --git a/core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/threads/name/CustomThreadName.java b/core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/threads/name/CustomThreadName.java new file mode 100644 index 0000000000..9ce35d1137 --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/threads/name/CustomThreadName.java @@ -0,0 +1,70 @@ +package com.baeldung.concurrent.threads.name; + +public class CustomThreadName { + + public int currentNumber = 1; + + public int N = 5; + + public static void main(String[] args) { + + CustomThreadName test = new CustomThreadName(); + + Thread oddThread = new Thread(() -> { + test.printOddNumber(); + // Uncomment below to set thread name using setName() Method + // Thread.currentThread().setName("ODD"); + }, "ODD"); + // or Uncomment below to set thread name using setName() Method + // oddThread.setName("ODD"); + + Thread evenThread = new Thread(() -> { + test.printEvenNumber(); + // Uncomment below to set thread name using setName() Method + // Thread.currentThread().setName("EVEN"); + }, "EVEN"); + + // evenThread.setName("EVEN"); + + evenThread.start(); + oddThread.start(); + + } + + public void printEvenNumber() { + synchronized (this) { + while (currentNumber < N) { + while (currentNumber % 2 == 1) { + try { + wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println(Thread.currentThread() + .getName() + " --> " + currentNumber); + currentNumber++; + notify(); + } + } + } + + public void printOddNumber() { + synchronized (this) { + while (currentNumber < N) { + while (currentNumber % 2 == 0) { + try { + wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println(Thread.currentThread() + .getName() + " --> " + currentNumber); + currentNumber++; + notify(); + } + } + } + +}