Thursday, December 23, 2010

Java-Multithreading



class Threading {
     public static void main(String[] args) {

           /*Method No.1
Counter ctr = new Counter();
           ctr.start();*/
          
           /*Method No.2*/
           Thread thread = new Thread(new Counter());
           thread.start();
     }
}

/*Method No.1
class Counter extends Thread {
     public void run() {
           int ctr = 1;

           while (ctr <= 10) {
                System.out.println(ctr++);
                try {
                     sleep(1000);
                } catch (InterruptedException ex) {
                }
           }
     }
}*/

/*Method No.2*/
class Counter implements Runnable {
     public void run() {
           int ctr = 1;

           while (ctr <= 10) {
                System.out.println(ctr++);
                try {
                     Thread.sleep(1000);
                } catch (InterruptedException ex) {
                }
           }
     }
}

0 comments:

Post a Comment

Contact Form

Name

Email *

Message *

The Insane Techie - Android App

Launched an android app for the blog on 07th June 2016. Get it from google play store... Tips for using the app Use in landscape mo...