Saturday, December 5, 2015

Threads for Beginners 2


Codes



class A {
      public static void main(String[] args) {
            System.out.println("Before");
            Thread.sleep(1000);
            System.out.println("After");
      }
}

-----------------------------------------------------------------------------------------------------------------


class A {
      public static void main(String[] args) {
            System.out.println("Before");
            try {
                  Thread.sleep(1000);
            } catch (Exception e) {
                  System.out.println(e);
            }
            System.out.println("After");
      }
}



-----------------------------------------------------------------------------------------------------------------

class A {
      void sleepPrograme() {
            try {
                  Thread.sleep(1000);
                  System.out.println("After");
            } catch (Exception e) {
                  System.out.println(e);
            }
      }

      public static void main(String[] args) {
            A a = new A();
            System.out.println("Before");
            a.sleepPrograme();
      }
}


-----------------------------------------------------------------------------------------------------------------


class A {
      public static void main(String[] args) {
            Thread t=Thread.currentThread();
            System.out.println(t.getName());
      }
}


-----------------------------------------------------------------------------------------------------------------

class A {
      public static void main(String[] args) {
            Thread t=Thread.currentThread();
            t.setName("My Thread");
            System.out.println(t.getName());
      }
}


-----------------------------------------------------------------------------------------------------------------

class A {
      public static void main(String[] args) {
            Thread t = Thread.currentThread();
            t.setName("My Thread");
            Thread t1 = Thread.currentThread();
            System.out.println(t1.getName());
      }
}


-----------------------------------------------------------------------------------------------------------------

class A {
      public static void main(String[] args) {
            Thread t1 = Thread.currentThread();
            System.out.println(t1.getId());
      }
}


-----------------------------------------------------------------------------------------------------------------

class A {
      public static void main(String[] args) {
            Thread t1 = Thread.currentThread();
            System.out.println(t1.getPriority());
      }
}


-----------------------------------------------------------------------------------------------------------------

class A {
      public static void main(String[] args) {
            Thread t1 = Thread.currentThread();
            t1.setPriority(3);
            System.out.println(t1.getPriority());
      }
}


-----------------------------------------------------------------------------------------------------------------

class A {
      public static void main(String[] args) {
            Thread t1 = Thread.currentThread();
            t1.setPriority(Thread.MIN_PRIORITY);
            System.out.println(t1.getPriority());
      }
}


-----------------------------------------------------------------------------------------------------------------

class A {
      public static void main(String[] args) {
            System.out.println(Thread.MIN_PRIORITY);
            System.out.println(Thread.NORM_PRIORITY);
            System.out.println(Thread.MAX_PRIORITY);
      }
}


-----------------------------------------------------------------------------------------------------------------

class A {
      public static void main(String[] args) {
            Thread t = Thread.currentThread();
            System.out.println(t.getState());
      }
}

-----------------------------------------------------------------------------------------------------------------

class A extends Thread {
      private int i;

      public void run() {
            i = 1;
      }

      public static void main(String[] args) {
            A a = new A();
            a.start();
            System.out.print(a.i);
      }
}


-----------------------------------------------------------------------------------------------------------------
class A extends Thread {
      private int i;

      public void run() {
            i = 1;
      }

      public static void main(String[] args) {
            A a = new A();
            a.run();
            System.out.print(a.i);
      }
}

-----------------------------------------------------------------------------------------------------------------

class A extends Thread {
      private int i;

      public void run() {
            System.out.println(Thread.currentThread().getName());
            i = 1;
      }

      public static void main(String[] args) {
            A a = new A();
            a.start();
            //System.out.println(Thread.currentThread().getName());
            try {
                  Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
            System.out.print(a.i);
            //System.out.println(Thread.currentThread().getName());
      }
}

-----------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

Using Zotero for academic writing