Created
October 24, 2018 18:47
-
-
Save finnkvan/085bcb7b7dd0e92f0345736a78b4e90e to your computer and use it in GitHub Desktop.
synchronized example
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; | |
| public class AsynDemo { | |
| synchronized public void countTo(int value) { | |
| int count = 0; | |
| while (count < value) { | |
| try { | |
| Thread.sleep(1000); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| count ++; | |
| System.out.println(Thread.currentThread() + " counted " + count); | |
| } | |
| } | |
| 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