BAEL-1263 Daemon Threads in Java
This commit is contained in:
parent
3ee473d30e
commit
ef4ee45a18
|
@ -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...");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue