System.out.printf(’%c’, char ch )

Tuesday, August 4th, 2009

/**
 Output:

 char value is a
 
 * */

public class MainClass {
  public static void main(String args[]) throws Exception {

    //char value is a
    System.out.printf("char value is %c\n", ‘a’ );

  }
}

           
       

Math.E

Thursday, July 16th, 2009

/*
 * Output:
 
E = 2.718281828459045

 */
public class MainClass {
  public static void main(String args[]) {
    System.out.println("E = " + Math.E);
  }
}
           
       

Double: equals

Monday, July 6th, 2009

/*
 * Output:

3.14159 = 3.14159 -> true
 * 
  
 */

public class MainClass {
  public static void main(String args[]) {
  Double d1 = new Double(3.14159);
  Double d2 = new Double("314159E-5");
  System.out.println(d1 + " = " + d2 + " -> " + d1.equals(d2));
  }
}

           
       

Integer: toHexString(int intValue)

Monday, July 6th, 2009

/*
 * Output:
 
Hex is b

 */

public class MainClass {
  public static void main(String args[]) {
    int i = 11;
    System.out.println("Hex is " + Integer.toHexString(i));
  }
}

           
       

ThreadGroup: enumerate(Thread[] thread)

Saturday, June 27th, 2009

/*
 * Output:
New thread: Thread[One,5,Group A]
New thread: Thread[Two,5,Group A]
One: 5
New thread: Thread[Three,5,Group B]
New thread: Thread[Four,5,Group B]

Here is output from list():
java.lang.ThreadGroup[name=Group A,maxpri=10]
    Thread[One,5,Group A]
    Thread[Two,5,Group A]
java.lang.ThreadGroup[name=Group B,maxpri=10]
    Thread[Three,5,Group B]
    Thread[Four,5,Group B]
Suspending Group A
Three: 5
Two: 5
Four: 5
Resuming Group A
Four: 4
Three: 4
Waiting for threads to finish.
Two: 4
 */

class MyThread extends Thread {
  boolean suspended;

  MyThread(String threadname, ThreadGroup tgOb) {
    super(tgOb, threadname);
    System.out.println("New thread: " + this);
    suspended = false;
    start(); // Start the thread
  }

  public void run() {
    try {
      for (int i = 5; i > 0; i–) {
        System.out.println(getName() + ": " + i);
        Thread.sleep(1000);
        synchronized (this) {
          while (suspended) {
            wait();
          }
        }
      }
    } catch (Exception e) {
      System.out.println("Exception in " + getName());
    }
    System.out.println(getName() + " exiting.");
  }

  void suspendMe() {
    suspended = true;
  }

  synchronized void resumeMe() {
    suspended = false;
    notify();
  }
}

public class MainClass {
  public static void main(String args[]) {
    ThreadGroup groupA = new ThreadGroup("Group A");
    ThreadGroup groupB = new ThreadGroup("Group B");

    MyThread ob1 = new MyThread("One", groupA);
    MyThread ob2 = new MyThread("Two", groupA);
    MyThread ob3 = new MyThread("Three", groupB);
    MyThread ob4 = new MyThread("Four", groupB);

    System.out.println("\nHere is output from list():");
    groupA.list();
    groupB.list();

    System.out.println("Suspending Group A");
    Thread tga[] = new Thread[groupA.activeCount()];
    groupA.enumerate(tga); // get threads in group
    for (int i = 0; i < tga.length; i++) {
      ((MyThread) tga[i]).suspendMe(); // suspend each thread
    }

    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      System.out.println("Main thread interrupted.");
    }

    System.out.println("Resuming Group A");
    for (int i = 0; i < tga.length; i++) {
      ((MyThread) tga[i]).resumeMe();
    }

    try {
      System.out.println("Waiting for threads to finish.");
      ob1.join();
      ob2.join();
      ob3.join();
      ob4.join();
    } catch (Exception e) {
      System.out.println("Exception in Main thread");
    }
    System.out.println("Main thread exiting.");
  }
}