Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ThreadsExample implements Runnable {
- static int counter = 1000; // a global counter
- public ThreadsExample() {
- }
- static synchronized void decrementCounter() {
- System.out.println(Thread.currentThread().getName() + ": " + counter);
- counter--;
- }
- @Override
- public void run() {
- while(counter>1){
- decrementCounter();
- }
- }
- public static void main(String[] args) {
- ThreadsExample te = new ThreadsExample();
- Thread thread1 = new Thread(te);
- Thread thread2 = new Thread(te);
- thread1.start();
- thread2.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement