Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Processor implements Runnable{
- private int id;
- public Processor(int id) {
- this.id = id;
- }
- @Override
- public void run() {
- int count=0;
- System.out.println("Starting process id: " + id);
- while(count<100) {
- System.out.println("Pausing process id: "+id);
- try {
- wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- notifyAll();
- System.out.println("Resuming process id: "+id);
- count++;
- }
- System.out.println("Completed process id: " + id);
- }
- }
- public class Test {
- @SuppressWarnings("resource")
- public static void main(String[] args) {
- Scanner reader = new Scanner(System.in);
- System.out.print("Enter number of processes you want to create: ");
- int n = reader.nextInt();
- ExecutorService executor = Executors.newFixedThreadPool(n);
- for(int i=1;i<=n; i++) {
- executor.submit(new Processor(i));
- }
- executor.shutdown();
- try {
- executor.awaitTermination(10, TimeUnit.MINUTES);
- } catch (InterruptedException e1) {
- e1.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment