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 cùng một lúc trong Java program.Thread còn được gọi là lightweight process.Nếu không có thread thì không tạo được 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.
---------------------------
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 Runnanble 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 Runnanble 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
Thread has been started
Hello from thread : Than chào Quý Ban-Holla Amigosos!
Than chào Quý Ban-Holla Amigosos!
Thread has been started.
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 object");
} };
thread1.start();
}
}
Output of method 1 and method 2.
*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 object.