Created
October 24, 2018 19:29
-
-
Save finnkvan/0dbb9c4dffefe74942f6689324c24c64 to your computer and use it in GitHub Desktop.
ReetranLook
This file contains hidden or 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 com.pivincii; | |
| import java.util.concurrent.locks.Lock; | |
| import java.util.concurrent.locks.ReentrantLock; | |
| public class AsynDemo { | |
| Lock lock = new ReentrantLock(); | |
| public void countTo(int value) { | |
| lock.lock(); | |
| try { | |
| int count = 0; | |
| while (count < value) { | |
| Thread.sleep(1000); | |
| count++; | |
| System.out.println(Thread.currentThread() + " counted " + count); | |
| } | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| lock.unlock(); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| AsynDemo asynDemo = new AsynDemo(); | |
| new Thread(() -> asynDemo.countTo(3)).start(); | |
| new Thread(() -> asynDemo.countTo(3)).start(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment