---
title: "JAVA中的线程"
author: "zzhblh"
date: "2012-04-17T22:41:43Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2012-05-09T15:44:07Z"
id: 1488857
url: "https://zzhblh.iteye.com/blog/1488857"
---

线程的概述

进程和线程是现代操作系统中两个必不可少的运行模型。在操作系统中可以有多个进程，这些进程包括系统进程（由操作系统内部建立的进程）和用户进程（由用户程序建立的进程）；一个进程中可以有一个或多个线程。进程和进程之间不共享内存，也就是说系统中的进程是在各自独立的内存空间中运行的。而一个进程中的线可以共享系统分派给这个进程的内存空间。







线程的建立

在Java中建立线程有两种方法，一种是继承Thread类，另一种是实现Runnable接口，并通过Thread和实现Runnable的类来建立线程，其实这两种方法从本质上说是一种方法，即都是通过Thread类来建立线程，并运行run方法的。但它们的大区别是通过继承Thread类来建立线程，虽然在实现起来更容易，但由于Java不支持多继承，因此，这个线程类如果继承了Thread，就不能再继承其他的类了，因此，Java线程模型提供了通过实现Runnable接口的方法来建立线程，这样线程类可以在必要的时候继承和业务有关的类，而不是Thread类。



```java
package mythread;      
public class Thread1 extends Thread    {        
public void run()        {            
System.out.println(this.getName());        
}        
public static void main(String[] args)        {            System.out.println(
Thread.currentThread().getName());            
Thread1 thread1 = new Thread1();            
Thread1 thread2 = new Thread1 ();            
thread1.start();            
thread2.start();        
}    
} 
```


上面代码的运行结果如下：
main
Thread-0
Thread-1



```java
package mythread;   
public class MyRunnable implements Runnable  {      
public void run()      {          
System.out.println(
Thread.currentThread().getName());      
}      
public static void main(String[] args)      {          
MyRunnable t1 = new MyRunnable();          
MyRunnable t2 = new MyRunnable();          
Thread thread1 = new Thread(t1, "MyThread1");          
Thread thread2 = new Thread(t2);          
thread2.setName("MyThread2");          
thread1.start();          
thread2.start();      
}  
}  
```

上面代码的运行结果如下：
MyThread1
MyThread2










线程的生命周期

// 开始线程  
public void start( ); 
 public void run( );   
// 挂起和唤醒线程  
public void resume( );     // 不建议使用  
public void suspend( );    // 不建议使用  
public static void sleep(long millis);  
public static void sleep(long millis, int nanos);   
// 终止线程  
public void stop( );       // 不建议使用  
public void interrupt( );   
// 得到线程状态  
public boolean isAlive( );  
public boolean isInterrupted( );  
public static boolean interrupted( );   
// join方法  
public void join( ) throws InterruptedException; 

一、创建并运行线程

```java
public class LifeCycle extends Thread  {      
public void run()      {          
int n = 0;          
while ((++n) < 1000);              
}             
public static void main(String[] args) throws Exception    {          
LifeCycle thread1 = new LifeCycle();          
System.out.println("isAlive: " + thread1.isAlive());          thread1.start();         
 System.out.println("isAlive: " + thread1.isAlive());          thread1.join();  // 等线程thread1结束后再继续执行           System.out.println("thread1已经结束!");          
System.out.println("isAlive: " + thread1.isAlive());      
}  
}  
```

上面代码的运行结果：
isAlive: false
isAlive: true
thread1已经结束!
isAlive: false

二、挂起和唤醒线程


waite()和notify()因为会对对象的“锁标志”进行操作，所以它们必须在synchronized函数或synchronized　block中进行调用。如果在non-synchronized函数或non-synchronized　block中进行调用，虽然能编译通过，但在运行时会发IllegalMonitorStateException的异常。



三、终止线程的方法

使用退出标志终止线程

```java
public class ThreadFlag extends Thread  {      
public volatile boolean exit = false;       
public void run()      
{          
while (!exit);      
}      
public static void main(String[] args) throws Exception      {          
ThreadFlag thread = new ThreadFlag();          
thread.start();          
sleep(5000); // 主线程延迟5秒          
thread.exit = true;  // 终止线程thread          
thread.join();          
System.out.println("线程退出!");      
}  
}  
```

在上面代码中定义了一个退出标志exit，当exit为true时，while循环退出，exit的默认值为false。在定义exit时，使用了一个Java关键字volatile，这个关键字的目的是使exit同步，也就是说在同一时刻只能由一个线程来修改exit的值。

线程的同步

synchronized     ：
要想解决“脏数据”的问题，最简单的方法就是使用synchronized关键字来使run方法同步public synchronized void run()  
也就是说，对于同一个Java类的对象实例，run方法同时只能被一个线程调用，并当前的run执行完后，才能被其他的线程调用。
1.  synchronized关键字不能继承。
虽然可以使用synchronized来定义方法，但synchronized并不属于方法定义的一部分，因此，synchronized关键字不能被继承。如果在父类中的某个方法使用了synchronized关键字，而在子类中覆盖了这个方法，在子类中的这个方法默认情况下并不是同步的，而必须显式地在子类的这个方法中加上synchronized关键字才可以。
2.在定义接口方法时不能使用synchronized关键字。
3.构造方法不能使用synchronized关键字。
4.synchronized关键字只能用来同步方法，不能用来同步类变量，如下面的代码也是错误的。
public synchronized int n = 0; 

synchronized(表达式){}  ：
注意：在使用synchronized块同步类方法时，非静态方法可以使用this来同步，而静态方法必须使用Class对象来同步。它们互不影响。当然，也可以在非静态方法中使用Class对象来同步静态方法。但在静态方法中不能使用this来同步非静态方法。这一点在使用synchronized块同步类方法时应注意。