Search

TreeMap example

    public class JavaTreeMapExample
   {
     public static void main(String[] args)
     {
      
       //create object of TreeMap
       TreeMap treeMap = new TreeMap();
      
       /*
       Add key value pair to TreeMap using,
       Object put(Object key, Object value) method of Java TreeMap class,
       where key and value both are objects
       put method returns Object which is either the value previously tied
       to the key or null if no value mapped to the key.
       */
       
       treeMap.put("One", new Integer(1));
       treeMap.put("Two", new Integer(2));
      
       /*
       Please note that put method accepts Objects. Java Primitive values CAN NOT
       be added directly to TreeMap. It must be converted to corrosponding
       wrapper class first.
       */
      
       //retrieve value using Object get(Object key) method of Java TreeMap class
       Object obj = treeMap.get("Two");
       System.out.println(obj);
      
       /*
       Please note that the return type of get method is an Object. The value must
       be casted to the original class.
       */
      
     }
  }



Thread Synchronization example in java

    // File Name : Callme.java
     // This program uses a synchronized block.
     class Callme
     {
       void call(String msg)
       {
         System.out.print("[" + msg);
         try
         {
           Thread.sleep(1000);
         }
         catch (InterruptedException e)
         {
           System.out.println("Interrupted");
         }
         System.out.println("]");
       }
     }
     // File Name : Caller.java
     class Caller implements Runnable
     {
       String msg;
       Callme target;
       Thread t;
       public Caller(Callme targ, String s)
       {
         target = targ;
         msg = s;
         t = new Thread(this);
         t.start();
       }
       // synchronize calls to call()
       public void run()
       {
         synchronized(target)
         {
           // synchronized block
           target.call(msg);
         }
       }
     }
     // File Name : Synch.java
     class Synch
     {
       public static void main(String args[])
       {
         Callme target = new Callme();
         Caller ob1 = new Caller(target, "Hello");
         Caller ob2 = new Caller(target, "Synchronized");
         Caller ob3 = new Caller(target, "World");
        
         // wait for threads to end
         try
         {
           ob1.t.join();
           ob2.t.join();
           ob3.t.join();
         }
         catch(InterruptedException e)
         {
           System.out.println("Interrupted");
         }
       }
     }


Create a thread by implementing Runnable

class MyThread implements Runnable
   {
     int count;
     MyThread()
     {
       count = 0;
     }
     public void run()
     {
       System.out.println("MyThread starting.");
       try
       {
         do
         {
           Thread.sleep(500);
           System.out.println("In MyThread, count is " + count);
           count++;
         } while (count < 5);
       } catch (InterruptedException exc)
       {
         System.out.println("MyThread interrupted.");
       }
       System.out.println("MyThread terminating.");
     }
   }
   class RunnableDemo
   {
     public static void main(String args[])
     {
       System.out.println("Main thread starting.");
       MyThread mt = new MyThread();
       Thread newThrd = new Thread(mt);
       newThrd.start();
       do
       {
         System.out.println("In main thread.");
         try
         {
           Thread.sleep(250);
         } catch (InterruptedException exc)
         {
           System.out.println("Main thread interrupted.");
         }
       } while (mt.count != 5);
       System.out.println("Main thread ending.");
     }
   }