You could do this easily in two ways:
-
Pass a ‘print token’ between the threads using two semaphores: thread 1 prints, signals semaphore A, waits on semaphore B then loops. Thread 2 waits on semaphore A, prints, signals semaphore B and loops.
-
Write in-line, single-threaded code.
More Related Contents:
- Why are wait() and notify() declared in Java’s Object class?
- How can the wait() and notify() methods be called on Objects that are not threads?
- Difference between Synchronized block with wait/notify and without them?
- Concept behind putting wait(),notify() methods in Object class [duplicate]
- Loop doesn’t see value changed by other thread without a print statement
- Waiting for multiple SwingWorkers
- Why must wait() always be in synchronized block
- IllegalMonitorStateException on wait() call
- A simple scenario using wait() and notify() in java
- Synchronizing on String objects in Java
- Synchronization vs Lock
- What is the difference between atomic / volatile / synchronized?
- How to wait for a number of threads to complete?
- How to synchronize a static variable among threads running different instances of a class in Java?
- How do I make my ArrayList Thread-Safe? Another approach to problem in Java?
- How do synchronized static methods work in Java and can I use it for loading Hibernate entities?
- Are Java static initializers thread safe?
- Java double checked locking
- Java Synchronized Block for .class
- Difference between WAIT and BLOCKED thread states
- What is the difference between a synchronized method and synchronized block in Java? [duplicate]
- running 3 threads in sequence java
- Sharing a variable between multiple different threads
- Should you synchronize the run method? Why or why not?
- How to wait for all tasks in an ThreadPoolExecutor to finish without shutting down the Executor?
- Difference between “wait()” vs “sleep()” in Java
- Java : Does wait() release lock from synchronized block
- Is synchronization within an HttpSession feasible?
- Java Delay/Wait
- who and when notify the thread.wait() when thread.join() is called?