Merge pull request #11588 from sachin071287/feature/bael-5271

BAEL-5271 set custom thread name
This commit is contained in:
Greg Martin 2022-01-23 14:33:46 -05:00 committed by GitHub
commit ff1ebafe37
1 changed files with 70 additions and 0 deletions

View File

@ -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();
}
}
}
}