THÂN CHÀO QUÝ BẠN
Blogger nầy chỉ tồn trử kiến thức và kinh nghiệm về nghề “Materials Testing”, “Textile Screen Printing” và “Internet Programming” của một kỹ sư đã phục vụ tại :
* Trung Tâm Khảo sát Kỹ Thuật Quân Nhu /QLVNCH “ & “ Viện Quốc Gia Định Chuẩn/VNCH”
* Xí nghiệp “Hiệp Hưng / VN” ( sãn xuất giày vãi cao su )
"Bradbury Company Inc/USA"(Textile Screenprinting,Imprinted Sportswear Programs) & “Sundance Graphics Inc / USA” ( in bông vãi sợi ) từ năm 1965 để dành cho thế hệ trẻ muốn học nghề.
Vạn Vật Thái Bình (PEACE ON EARTH).

Saturday, June 6, 2015

UNDERSTANDING THREADS USED IN JAVA PROGRAM

TÌM HIỂU NHỮNG SỢI CHỈ DỆT THÀNH JAVA PROGRAM.
 
GHI CHÚ HƯỚNG DẪN.
 
Người phát minh Java programming xem Java program giống như một tấm vải dệt bằng những sợi chỉ gọi là THREADS.
Thread là tên gọi của mỗi phần (part) vận hành theo một việc (task) riêng rẽ nhưng song hành với những thread khác trong Java program.
Khi những Threads được vận hành ( executed) sẽ giống như những sợi chỉ dệt thành vãi trong ngành dệt vãi sợi để tạo ra fabric of program.
Trong mỗi Java program có thể chứa 2 hay nhiều threads và những threads nầy vận hành cùng một lúc khi được called để xử dụng tôi đa CPU, không cho CPU rảnh rỗi.
Trong Java class có cái viết giống method gọi là constructor.Constructor phải có tên giống hệt tên của class dùng để dành chỗ trong memory cho object cho nên constructor còn được gọi là initialization of new object.
Constructor là một “block of code” có ký hiệu viết giống hệt method nhưng không phải method vì không có return type.Method thì có.
Thí du. Rextester(); Baxao() ; Xaoke(); Box (); Car(); Dog()
Tất cả đều là constructors và phải có tên giống như tên của class.
Dùng constructor để tạo ra object bằng cách viết thêm chữ new trước constructor.
 new Baxao(); new Box ();  new Car(); rồi  viết dấu bằng  “=”  với tên của class.Thí du.
class Baxao{  } thi object của class sẽ là Baxao  bx = new Baxao();
bx là variable do tùy ý chúng ta chọn bất cứ chữ gì.
                                     ---------------------------
Each part of a Java program is called a thread. Each thread has a separate path of execution and can run concurrently, simultaneously with others .We put threads in Java program in order to keep the idle time of CPU to minimum.
    When the program starts running, the main thread and the GUI thread will  run simultaneously .                                
 Java classes have special method called constructor. Constructor must have the same name as the class and is used to initialize a new object. 
                                     -----------------------------
Có hai phương pháp để tạo ra Java thread . 
   * Runnable interface,còn được gọi Implement Runnable .
   *  Extending Thread class.
 Cả hai phương pháp đều cần phải tạo thread object là :
                     Thread thread = new Thread ().
 
1-Phương pháp runnable interface
 1.1- Thí dụ.
 
class Rextester{  
  public static void main(String args[]){
            Thread thread1 = new Thread (){     
  public void run() { 
  System.out.println("Method 1:runnable interface.Thread is running \n Everything is inside main method");
      } };
              thread1.start();
     }
    }
Output.
    Compilation time: 0.83 sec, absolute running time: 0.13 sec, 
cpu time: 0.08 sec, memory peak: 22 Mb, absolute service time: 0.97 sec
 
Method 1:runnable interface.Thread is running 
 Everything is inside main method

1.2- Thí dụ

/*class must have  2 words “implements Runnable” */
class Rextester implements Runnable{     
public void run(){
System.out.println("Hello Friends.Thread of Method Runnable is running");}
public static void main(String args[]){
  Rextester T = new Rextester();
      Thread thread = new Thread(T);
      thread.start();
  }
}
Output.
Compilation time: 0.83 sec, absolute running time: 0.14 sec, 
cpu time: 0.08 sec, memory peak: 21 Mb, absolute service time: 0.97 sec
 
Hello Friends.Thread of Method Runnable is running
 
2- Phương pháp extending Thread class.
     
 2.1-Thí dụ.
 
/*class must have 2 words “ extends Thread ”  */
class Rextester extends Thread{ 
    public void run(){
System.out.println("Method 2:using extends Thread \n Thread is running");} 
    public static void main(String args[]){
    Rextester t2 = new Rextester();
    t2.start();
}
 }
Output.
Compilation time: 0.72 sec, absolute running time: 0.13 sec, 
cpu time: 0.07 sec, memory peak: 23 Mb, absolute service time: 0.86 sec
 
Method 2:using extends Thread 
 Thread is running
 
2.2-Thí dụ xử dụng chữ “this” trong class extends Thread.
        Dùng chữ this để đề cập tới( refer to) object trong constructor.
       Tên của constructor phải giống hệt như tên của class. Constructror không phải là method mặc dầu viết giống method vì constructor không có return type.Còn method thì có.
 
class Rextester extends Thread {
   private String name;  
   public Rextester(String name) { // Đây là constructor
      this.name = name;}
   public void run() {  
      System.out.println(" Hello from thread : " + name + "!"); }
   public static void main(String args[]){  
  Rextester  thread = new Rextester(" Than chào Quý Ban-Holla Amigosos");
                    thread.start(); 
  System.out.println("Thread has been started");}
   }
OUTPUT.
Compilation time: 0.73 sec, absolute running time: 0.14 sec, 
cpu time: 0.1 sec, memory peak: 23 Mb, absolute service time: 0.88 sec
Thread has been started
 Hello from thread :  Than chào Quý Ban-Holla Amigosos!
 
GHI CHÚ.
Khi thread.start() vận hành thì có 2 threads xuất hiện :
  * một thread in ra câu Than chào Quý Ban-Holla Amigosos!
* một thread in ra câu Thread has been started.
Nếu class có chứa nhiều threads ,chúng ta sẽ không biết chắc chắn câu nào được in ra trước và câu nào được in ra sau.
 

3-Phương pháp 1 và 2 viết chung trong một class.
  Class Rextester extends Thread cũng vận hành được  phương pháp 1 
     (runnable interface).
          Thí dụ.
 
class Rextester extends Thread{ 
    public void run(){
System.out.println("Method 2:using extends Thread .Thread is running and remember run method is located outside main method");} 
    public static void main(String args[]){
    Rextester t2 = new Rextester();
    t2.start();
 
    Thread thread1 = new Thread (){     
  public void run() { 
  System.out.println("Method 1:runnable interface.Thread is running \n Everything is inside main method . Remember run method is embedded in Thread constructor");
      } };
              thread1.start();
     }
}
Output of method 1 and method 2.
 Compilation time: 0.72 sec, absolute running time: 0.13 sec, 
cpu time: 0.1 sec, memory peak: 23 Mb, absolute service time: 0.86 sec
 
*Method 2:using extends Thread .Thread is running and remember run method is located outside main method
*Method 1:runnable interface.Thread is running .
 Everything is inside main method . Remember run method is embedded in Thread constructor.
 Ghi chú.
Trong class nầy chúng ta xử dụng 2 lần public void run()  nên không tốt 
lắm gọi là non sense.Do đó chúng ta đã có cách dấu public void run() 
trong thread trong thí dụ sau.