Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package multithreading.lock;
- public class ReentrantSyncrhonizedBlock{
- public synchronized void outer() throws InterruptedException {
- System.out.println(Thread.currentThread().getName() + " enters OUTER");
- Thread.sleep(500);
- inner();
- }
- private synchronized void inner() {
- System.out.println(Thread.currentThread().getName() + " enters INNER");
- }
- }
- package multithreading.lock;
- public class ClientReentrantSynchronizedBlock {
- public static void main(String[] args) throws InterruptedException {
- ReentrantSyncrhonizedBlock reentrant = new ReentrantSyncrhonizedBlock();
- for (int i = 0; i < 5; i++) {
- new Thread(() -> {
- try {
- reentrant.outer();
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }, "Thread " + i).start();
- }
- Thread.sleep(7000); // needed to wait all threads to be completed (I'm lazy to call join on them after starting =) )
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement