15 April 2017

Java: Đa luồng trong Java Multithreading in Java

1)New

The thread is in new state if you create an instance of Thread class but before the invocation of start() method.

2)Runnable

The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.

3)Running

The thread is in running state if the thread scheduler has selected it.

4)Non-Runnable (Blocked)

This is the state when the thread is still alive, but is currently not eligible to run.

5)Terminated

A thread is in terminated or dead state when its run() method exits.

Làm thế nào để tạo Thread:

Chúng ta có hai cách tạo Thread:
  1. Bằng extending Thread class
  2. Bằng implementing Runnable interface.

Thread class:

Lớp Thread cung cấp Constructors và Methods để tạo và thực hiện các thao tác trên một luồng.
Sử dụng Constructors của Thread:

  • Thread()
  • Thread(String name)
  • Thread(Runnable r)
  • Thread(Runnable r,String name)

Sử dụng Methods của Thread:

  1. public void run(): is used to perform action for a thread.
  2. public void start(): starts the execution of the thread.JVM calls the run() method on the thread.
  3. public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
  4. public void join(): waits for a thread to die.
  5. public void join(long miliseconds): waits for a thread to die for the specified miliseconds.
  6. public int getPriority(): returns the priority of the thread.
  7. public int setPriority(int priority): changes the priority of the thread.
  8. public String getName(): returns the name of the thread.
  9. public void setName(String name): changes the name of the thread.
  10. public Thread currentThread(): returns the reference of currently executing thread.
  11. public int getId(): returns the id of the thread.
  12. public Thread.State getState(): returns the state of the thread.
  13. public boolean isAlive(): tests if the thread is alive.
  14. public void yield(): causes the currently executing thread object to temporarily pause and allow other threads to execute.
  15. public void suspend(): is used to suspend the thread(depricated).
  16. public void resume(): is used to resume the suspended thread(depricated).
  17. public void stop(): is used to stop the thread(depricated).
  18. public boolean isDaemon(): tests if the thread is a daemon thread.
  19. public void setDaemon(boolean b): marks the thread as daemon or user thread.
  20. public void interrupt(): interrupts the thread.
  21. public boolean isInterrupted(): tests if the thread has been interrupted.
  22. public static boolean interrupted(): tests if the current thread has been interrupted.

Runnable interface:

Giao diện Runnable chỉ có một phương thức có tên run ().
  1. public void run(): Được sử dụng để thực hiện hành động cho Thread.

1) Bằng extending Thread class:

  1. class Multi extends Thread{  
  2.      public void run(){  
  3.          System.out.println("thread is running...");  
  4.     }  
  5.      public static void main(String args[]){  
  6.          Multi t1=new Multi();  
  7.          t1.start();  
  8.     }  
  9. }  
Output:thread is running...

2) Bằng implementing Runnable interface:

  1. class Multi3 implements Runnable{  
  2.       public void run(){  
  3.           System.out.println("thread is running...");  
  4.      }  
  5.       public static void main(String args[]){  
  6.           Multi3 m1=new Multi3();  
  7.           Thread t1 =new Thread(m1);  
  8.           t1.start();  
  9.      }  
  10. }  
Output:thread is running...

Tìm hiểu thêm tại đây

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang