4.4 Thread

来源:互联网 发布:退出淘宝客有什么影响 编辑:程序博客网 时间:2024/05/22 13:32

4.4.1 线程与进程

线程

定义:程序中单独顺序的控制流。

线程本身依靠进程进行运行,线程是程序中的顺序控制流,只能使用分配给进程的资源和环境。

进程

定义:执行中的程序。

一个进程可以包括一个或者多个线程。

一个进程至少包括一个线程。

单线程程序

进程中只有一个线程,main方法就是主线程。

多线程程序

在一个程序中运行多个任务。多线程的目的是更好的使用cpu资源。

4.4.2 线程的实现

在java中,线程的实现有2中方式:继承Thread类和实现Runnable接口。

一个Thread对象只能调用一次start方法,否则exception。

Thread类

java.lang.Thread

必须重写run方法

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class MyThread extends Thread{  
  2.     private String name;  
  3.     public MyThread(String name) {  
  4.         this.name = name;  
  5.     }  
  6.   
  7.     @Override  
  8.     public void run() {  
  9.         for (int i = 0; i < 1000; i++) {  
  10.             System.out.println(name + ":"+ i);  
  11.         }  
  12.     }  
  13. }  

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void main(String[] args) {  
  2.     MyThread t1 = new MyThread("t1");  
  3.     MyThread t2 = new MyThread("t2");  
  4.     t1.start();  
  5.     t2.start();  
  6.     //如果这里调用run方法,不是启用线程,而是方法调用!!!  
  7. }  

Runnable接口

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class MyThread2 implements Runnable{  
  2.     private String name;  
  3.     public MyThread2(String name) {  
  4.         this.name = name;  
  5.     }  
  6.     @Override  
  7.     public void run() {  
  8.         for (int i = 0; i < 1000; i++) {  
  9.             System.out.println(name + ":"+ i);  
  10.         }  
  11.     }  
  12. }  

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void main(String[] args) {  
  2.     MyThread2 t1 = new MyThread2("t1");  
  3.     MyThread2 t2 = new MyThread2("t2");  
  4.     new Thread(t1).start();  
  5.     new Thread(t2).start();  
  6.     //如果这里调用run方法,不是启用线程,而是方法调用!!!  
  7. }  

4.4.3 线程的状态

  1. 创建状态:准备好了一个thread对象
  2. 就绪状态:调用了start方法,加入cpu的调度序列
  3. 运行状态:正在执行run方法
  4. 阻塞状态:暂时停止执行
  5. 终止状态:也叫死亡状态,线程销毁

4.4.4 线程的常用方法

  • getName() : 取得线程名称
  • Thread.currentThread(); 取得当前运行的线程对象
    [java] view plaincopy在CODE上查看代码片派生到我的代码片
    1. class MyThread2 implements Runnable{  
    2.     private String name;  
    3.   
    4.     public String getName() {  
    5.         return name;  
    6.     }  
    7.   
    8.     public MyThread2(String name) {  
    9.         this.name = name;  
    10.     }  
    11.     @Override  
    12.     public void run() {  
    13.         for (int i = 0; i < 1000; i++) {  
    14.             System.out.println(Thread.currentThread().getName()+ ":"+ i);  
    15.         }  
    16.     }  
    17. }  
    [java] view plaincopy在CODE上查看代码片派生到我的代码片
    1. public static void main(String[] args) {  
    2.     MyThread2 r1 = new MyThread2("t1");  
    3.     MyThread2 r2 = new MyThread2("t2222222222222");  
    4.     Thread t1 = new Thread(r1, r1.getName());  
    5.     Thread t2 = new Thread(r2, r2.getName());  
    6.     t1.start();  
    7.     t2.start();  
    8. }  
  • isAlive(); 判断线程是否启动,start之前是false
  • join(); 线程的强行运行,执行完之后才把cpu让给其他线程。
    [java] view plaincopy在CODE上查看代码片派生到我的代码片
    1. class MyThread2 implements Runnable{  
    2.     private String name;  
    3.   
    4.     public String getName() {  
    5.         return name;  
    6.     }  
    7.   
    8.     public MyThread2(String name) {  
    9.         this.name = name;  
    10.     }  
    11.     @Override  
    12.     public void run() {  
    13.         for (int i = 0; i < 1000; i++) {  
    14.             System.out.println(Thread.currentThread().getName()+ ":"+ i);  
    15.         }  
    16.     }  
    17. }  

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
    1. public static void main(String[] args) {  
    2.     MyThread2 r1 = new MyThread2("t1");  
    3.     Thread t = new Thread(r1);  
    4.     t.setName(r1.getName());  
    5.     t.start();  
    6.     for (int i = 0; i < 50; i++) {  
    7.         if(i > 10){  
    8.   
    9.             try {  
    10.                 t.join();  
    11.             } catch (InterruptedException e) {  
    12.                 e.printStackTrace();  
    13.             }  
    14.         }  
    15.         System.out.println("in main thread : " + i);  
    16.     }  
    17. }  

    输出结果
    [java] view plaincopy在CODE上查看代码片派生到我的代码片
    1. in main thread : 0  
    2. in main thread : 1  
    3. in main thread : 2  
    4. in main thread : 3  
    5. t1:0  
    6. in main thread : 4  
    7. t1:1  
    8. in main thread : 5  
    9. t1:2  
    10. in main thread : 6  
    11. in main thread : 7  
    12. in main thread : 8  
    13. in main thread : 9  
    14. t1:3  
    15. in main thread : 10  
    16. t1:4  
    17. t1:5  
    18. t1:6  
    19. t1:7  
    20. t1:8  
    21. t1:9  
    22. 一直到  
    23. t1:996  
    24. t1:997  
    25. t1:998  
    26. t1:999  
    27. in main thread : 11  
    28. in main thread : 12  
    29. in main thread : 13  
    30. in main thread : 14  
    31. 。。。。。  
    32. in main thread : 45  
    33. in main thread : 46  
    34. in main thread : 47  
    35. in main thread : 48  
    36. in main thread : 49  

  • Thread.sleep(millis); 线程的休眠
  • Thread.yield(); 从执行态到就绪态,等待调度。

4.4.5 线程的优先级

优先级顺序:

  • MIN_PRIORITY : 1
  • MAX_PRIORITY: 10
  • NORMAL_PROORITY: 5 (default)


4.4.6 线程的同步 : 多个线程操纵共享数据的时候需要同步 

同步代码块

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. synchronized (同步对象){  
  2.     需要同步的代码块;  
  3. }  

同步方法

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. synchronized void synchronizedMethod(){  
  2.     xxxxx;  
  3. }  

买车票的问题

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class SellTicket implements Runnable{  
  2.     private int numTickets = 5;  
  3.   
  4.     @Override  
  5.     public void run() {  
  6.             synchronized (this){  
  7.                 System.out.println(--numTickets);  
  8.             }  
  9.     }  
  10. }  

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void main(String[] args) {  
  2.     SellTicket r = new SellTicket();  
  3.     Thread window1 = new Thread(r);  
  4.     Thread window2 = new Thread(r);  
  5.     Thread window3 = new Thread(r);  
  6.     window1.start();  
  7.     window2.start();  
  8.     window3.start();  
  9. }  

同步方法

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class SellTicket implements Runnable{  
  2.     private int numTickets = 5;  
  3.   
  4.     @Override  
  5.     public void run() {  
  6.         decrementTicket();  
  7.     }  
  8.   
  9.     public synchronized void decrementTicket(){  
  10.         System.out.println(--numTickets);  
  11.     }  
  12. }  

4.4.7 线程的生命周期

0 0
原创粉丝点击