Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- class user{
- public String name;
- }
- class Thread1 implements Runnable
- {
- Thread mythread ;
- ArrayList<user> list;
- Thread1(ArrayList<user> l)
- {
- list = l;
- mythread = new Thread(this, "Thread1");
- System.out.println("Thread1 created");
- mythread.start();
- }
- public void run()
- {
- try
- {
- for (int i=0 ;list.size()<1000;i++)
- {
- list.add(new user());
- System.out.println("Adding items " + i);
- Thread.sleep(1);
- }
- }
- catch(InterruptedException e)
- {
- System.out.println("Thread1 interrupted");
- }
- System.out.println("Thread1 run is over" );
- }
- }
- class Thread2 implements Runnable
- {
- Thread mythread ;
- ArrayList<user> list;
- Thread2(ArrayList<user> l)
- {
- mythread = new Thread(this, "Thread2");
- System.out.println("Thread2 created");
- list = l;
- mythread.start();
- }
- public void run()
- {
- try
- {
- for (int i=0 ;i<1000;i++)
- {
- System.out.println("Removing objects " + i);
- list.remove(list.size() -1);
- Thread.sleep(1);
- }
- }
- catch(InterruptedException e)
- {
- System.out.println("Thread2 interrupted");
- }
- System.out.println("Thread2 run is over" );
- }
- }
- public class DemoThread
- {
- public static void main(String args[])
- {
- ArrayList<user> list = new ArrayList<user>();
- Thread1 t1 = new Thread1(list);
- Thread2 t2 = new Thread2(list);
- try
- {
- while(t1.mythread.isAlive() || t2.mythread.isAlive())
- {
- System.out.println(" Size : " + list.size());
- Thread.sleep(1);
- }
- }
- catch(InterruptedException e)
- {
- System.out.println("Main thread interrupted");
- }
- System.out.println("Main thread run is over" );
- }
- }
- After 6
- import java.io.*;
- import java.util.*;
- class user{
- public String name;
- }
- class Thread1 implements Runnable
- {
- Thread mythread ;
- ArrayList<user> list;
- Thread1(ArrayList<user> l)
- {
- list = l;
- mythread = new Thread(this, "Thread1");
- System.out.println("Thread1 created");
- mythread.start();
- }
- public void run()
- {
- try
- {
- for (int i=0 ;list.size()<1000;i++)
- {
- while(list.size() == 1000){
- Thread.sleep(1);
- }
- list.add(new user());
- System.out.println("Adding items " + i);
- }
- }
- catch(InterruptedException e)
- {
- System.out.println("Thread1 interrupted");
- }
- System.out.println("Thread1 run is over" );
- }
- }
- class Thread2 implements Runnable
- {
- Thread mythread ;
- ArrayList<user> list;
- Thread2(ArrayList<user> l)
- {
- mythread = new Thread(this, "Thread2");
- System.out.println("Thread2 created");
- list = l;
- mythread.start();
- }
- public void run()
- {
- try
- {
- for (int i=0 ;i<1000;i++)
- {
- System.out.println("Removing objects " + i);
- while (list.size() == 0){
- Thread.sleep(1);
- }
- list.remove(list.size() -1);
- }
- }
- catch(InterruptedException e)
- {
- System.out.println("Thread2 interrupted");
- }
- System.out.println("Thread2 run is over" );
- }
- }
- public class DemoThread
- {
- public static void main(String args[])
- {
- ArrayList<user> list = new ArrayList<user>();
- Thread1 t1 = new Thread1(list);
- Thread2 t2 = new Thread2(list);
- try
- {
- while(t1.mythread.isAlive() || t2.mythread.isAlive())
- {
- System.out.println(" Size : " + list.size());
- Thread.sleep(1);
- }
- }
- catch(InterruptedException e)
- {
- System.out.println("Main thread interrupted");
- }
- System.out.println("Main thread run is over" );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement