Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- import java.util.Random;
- public class Console {
- public static void main(String[] args) throws InterruptedException {
- System.out.println("asd");
- final Queue<DemoThread> queue = new LinkedList<>();
- ExecutorService user = Executors.newCachedThreadPool();
- ExecutorService processor = Executors.newSingleThreadExecutor();
- user.submit(new Runnable(){
- public void run(){
- while (!Thread.currentThread().isInterrupted()) {
- if(!queue.isEmpty()){
- DemoThread temp = queue.poll();
- System.out.println(temp);
- processor.submit(temp);
- }
- }
- }
- });
- user.submit(new Runnable() {
- @Override
- public void run() {
- System.out.println("asdasdasdasdasdasdasd");
- queue.add(new DemoThread(2000));
- queue.add(new DemoThread(500));
- queue.add(new DemoThread(3000));
- queue.add(new DemoThread(100));
- System.out.println(queue);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- queue.add(new DemoThread(1666));
- System.out.println(queue);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment