adsense


Showing posts with label JAVA EXAMPLES CHECKING A THREAD. Show all posts
Showing posts with label JAVA EXAMPLES CHECKING A THREAD. Show all posts

Sunday, 1 November 2015

JAVA EXAMPLES CHECKING A THREAD PAGE 194

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES CHECKING A THREAD







Java Examples - Checking a Thread



Problem Description:
How to check priority level of a thread?
Solution:
Following example demonstrates how to check priority level 
of a thread using getPriority() method of a Thread.


public class Main extends Object {
   private static Runnable makeRunnable() {
      Runnable r = new Runnable() {
         public void run() {
            for (int i = 0; i < 5; i++) {
               Thread t = Thread.currentThread();
               System.out.println("in run() - priority="
               + t.getPriority()+ ", name=" + t.getName());
               try {
                  Thread.sleep(2000);
               }
               catch (InterruptedException x) {
               }
            }
         }
      };
      return r;
   }
   public static void main(String[] args) {
      System.out.println("in main() - Thread.currentThread().
      getPriority()=" + Thread.currentThread().getPriority());
      System.out.println("in main() - Thread.currentThread()
      .getName()="+ Thread.currentThread().getName());
      Thread threadA = new Thread(makeRunnable(), "threadA");
      threadA.start();
      try {
         Thread.sleep(3000);
      }
      catch (InterruptedException x) {
      }
      System.out.println("in main() - threadA.getPriority()="
      + threadA.getPriority());
   }
}
Result:
The above code sample will produce the following result.
in main() - Thread.currentThread().getPriority()=5
in main() - Thread.currentThread().getName()=main
in run() - priority=5, name=threadA
in run() - priority=5, name=threadA
in main() - threadA.getPriority()=5
in run() - priority=5, name=threadA
in run() - priority=5, name=threadA
in run() - priority=5, name=threadA


Saturday, 31 October 2015

JAVA EXAMPLES CHECKING A THREAD PAGE 185

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES CHECKING A THREAD








Java Examples - Checking a Thread


Problem Description:
How to check a thread is alive or not?
Solution:
Following example demonstrates how to check a thread is alive or not by extending Threda class and using currentThread() method.
public class TwoThreadAlive extends Thread {
   public void run() {
      for (int i = 0; i < 10; i++) {
      printMsg();
   }
}

public void printMsg() {
   Thread t = Thread.currentThread();
   String name = t.getName();
   System.out.println("name=" + name);
}

public static void main(String[] args) {
   TwoThreadAlive tt = new TwoThreadAlive();
   tt.setName("Thread");
   System.out.println("before start(), 
   tt.isAlive()=" + tt.isAlive());
   tt.start();
   System.out.println("just after start(), 
   tt.isAlive()=" + tt.isAlive());
   for (int i = 0; i < 10; i++) {
      tt.printMsg();
   }
   System.out.println("The end of main(),
      tt.isAlive()=" + tt.isAlive());
   }
}
Result:
The above code sample will produce the following result.
before start(),tt.isAlive()=false
just after start(), tt.isAlive()=true
name=main 
name=main 
name=main 
name=main 
name=main 
name=Thread
name=Thread 
name=Thread 
name=Thread
name=Thread 
name=Thread 
name=Thread 
name=main 
name=main 
The end of main().tt.isAlive()=false