Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class AsyncPrinter extends Thread
- {
- private Queue<String> queue;
- public AsyncPrinter()
- {
- this.queue=new LinkedList<>();
- this.setDaemon(true);
- this.start();
- }
- public void run()
- {
- String name;
- while(true)
- {
- while((name=this.queue.poll())!=null)
- {
- System.out.print("Hello, ");
- System.out.println(name);
- }
- try
- {
- this.wait();
- }
- catch(Exception ex)
- {
- }
- }
- }
- public boolean add(String name)
- {
- boolean ret=queue.offer(name);
- try
- {
- this.notifyAll();
- }
- catch(IllegalMonitorStateException ex)
- {
- }
- return ret;
- }
- public static void main(String... args)
- {
- AsyncPrinter printer=new AsyncPrinter();
- printer.add("test");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement