Last active
November 14, 2017 08:35
-
-
Save Pavneet-Sing/23bd944e322ebf02f32ac2cfa9c53fee to your computer and use it in GitHub Desktop.
Threads with lambda demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.myapplication; | |
import static org.junit.Assert.*; | |
/** | |
* Created by Pavneet Singh on 11/14/2017. | |
* Email : Pavneet.edu@gmail.com | |
*/ | |
public class Test { | |
@org.junit.Test | |
public void testing(){ | |
// assert id and name of main thread | |
assertMainNameAndID(Thread.currentThread().getName(),Thread.currentThread().getId()); | |
// assert id and name of worker thread 1 | |
new Thread(()->{ | |
assertWorkerNameAndID(Thread.currentThread().getName(),Thread.currentThread().getId()); | |
}).start(); | |
// No thread will be created , assert same value as first case | |
new Thread(()->{ | |
assertMainNameAndID(Thread.currentThread().getName(),Thread.currentThread().getId()); | |
}).run(); | |
// assert id and name of worker thread 2 | |
new Thread(()->{ | |
assertWorkerNameAndID(Thread.currentThread().getName(),Thread.currentThread().getId()); | |
}).start(); | |
} | |
private void assertMainNameAndID(String name, long id){ | |
assertEquals("Main thread ","main",name); | |
assertTrue(id==1); | |
} | |
private void assertWorkerNameAndID(String name, long id){ | |
assertTrue("Worker Thread "+id,name.matches("Thread-\\d+")); | |
assertTrue(id>1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment