Application class for Generic Stack Implementation



class MyThread extends Thread {
 
    MyGenericStack<Integer> iobj;
    String thname;
 
    public MyThread(MyGenericStack<Integer> iobj, String n) {
        this.iobj = iobj;
        thname=n;
    }
 
 
 
    public void run() {
         int no=1001;
       for (int i=0;i<40;i++)
        {
            if (thname.equals("1")) {
       
                if (iobj.push(new Integer(no))==true) {
                    System.out.println("Thread : "+ thname + " has pushed... " + (no++) );
                }
            }
            else if (thname.equals("2")) {
                Object myin = iobj.pop();
                System.out.println("Thread : "+ thname + " has poped... "+ myin);
         
            }
     
        }
     
       // iobj.show();  //  show method of MyGenericStack class
    }
}
public class MyThreadStackExample {
    public static void main(String[] args) {
 
 // The object of  MyGenericStack can hold elements of any type e.g. String,Double,Student, etc...

      MyGenericStack<Integer> mys = new MyGenericStack<Integer>(20);
   
   
     System.out.println("Main Started..");
       
     MyThread pt = new MyThread(mys,"1");
   
     MyThread pt2 = new MyThread(mys,"2");
   
       pt.start();
       pt2.start();
   
 
   
        try {
            pt.join();
           pt2.join();
   
        } catch (Exception e) {
        }
   
        System.out.println("Main finished..");
   
   
    }
}

Comments

Popular posts from this blog

Custom Exception example for BankAccount

Annotation

Interface with class and interface as member