BAEL-1263 Daemon Threads in Java

This commit is contained in:
Ahmed Tawila 2017-10-26 11:48:45 +02:00
parent 3ee473d30e
commit ef4ee45a18
2 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,9 @@
package com.baeldung.concurrent.daemon;
public class NewThread extends Thread {
public void run() {
for (int i = 0; i < 10; i++)
System.out.println("New Thread is running...");
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.concurrent.daemon;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class DaemonThreadTest {
@Test
public void whenCallIsDaemon_thenCorrect() {
NewThread daemonThread = new NewThread();
NewThread userThread = new NewThread();
daemonThread.setDaemon(true);
daemonThread.start();
userThread.start();
assertTrue(daemonThread.isDaemon());
assertFalse(userThread.isDaemon());
}
@Test(expected = IllegalThreadStateException.class)
public void givenUserThread_whenSetDaemonWhileRunning_thenIllegalThreadStateException() {
NewThread daemonThread = new NewThread();
daemonThread.start();
daemonThread.setDaemon(true);
}
@Test
public void givenUserThread_whenStartThread_thenFalse() {
NewThread daemonThread = new NewThread();
}
}