当前位置: 移动技术网 > IT编程>开发语言>Java > 总结Java中线程的状态及多线程的实现方式

总结Java中线程的状态及多线程的实现方式

2019年07月22日  | 移动技术网IT编程  | 我要评论

线程的状态
线程状态图:

20167484827701.jpg (700×480)

说明:
线程共包括以下5种状态。
1. 新建状态(new) : 线程对象被创建后,就进入了新建状态。例如,thread thread = new thread()。
2. 就绪状态(runnable): 也被称为“可执行状态”。线程对象被创建后,其它线程调用了该对象的start()方法,从而来启动该线程。例如,thread.start()。处于就绪状态的线程,随时可能被cpu调度执行。
3. 运行状态(running) : 线程获取cpu权限进行执行。需要注意的是,线程只能从就绪状态进入到运行状态。
4. 阻塞状态(blocked) : 阻塞状态是线程因为某种原因放弃cpu使用权,暂时停止运行。直到线程进入就绪状态,才有机会转到运行状态。阻塞的情况分三种:
(1) 等待阻塞 -- 通过调用线程的wait()方法,让线程等待某工作的完成。
(2) 同步阻塞 -- 线程在获取synchronized同步锁失败(因为锁被其它线程所占用),它会进入同步阻塞状态。
(3) 其他阻塞 -- 通过调用线程的sleep()或join()或发出了i/o请求时,线程会进入到阻塞状态。当sleep()状态超时、join()等待线程终止或者超时、或者i/o处理完毕时,线程重新转入就绪状态。
5. 死亡状态(dead) : 线程执行完了或者因异常退出了run()方法,该线程结束生命周期。
这5种状态涉及到的内容包括object类, thread和synchronized关键字。这些内容我们会在后面的章节中逐个进行学习。
object类,定义了wait(), notify(), notifyall()等休眠/唤醒函数。
thread类,定义了一些列的线程操作函数。例如,sleep()休眠函数, interrupt()中断函数, getname()获取线程名称等。
synchronized,是关键字;它区分为synchronized代码块和synchronized方法。synchronized的作用是让线程获取对象的同步锁。
在后面详细介绍wait(),notify()等方法时,我们会分析为什么“wait(), notify()等方法要定义在object类,而不是thread类中”。

实现多线程的两种方式:thread和runnable
runnable 是一个接口,该接口中只包含了一个run()方法。它的定义如下:

public interface runnable {
  public abstract void run();
}

runnable的作用,实现多线程。我们可以定义一个类a实现runnable接口;然后,通过new thread(new a())等方式新建线程。
thread 是一个类。thread本身就实现了runnable接口。它的声明如下:
public class thread implements runnable {}
thread的作用,实现多线程。

thread和runnable的异同点:
thread 和 runnable 的相同点:都是“多线程的实现方式”。
thread 和 runnable 的不同点:
thread 是类,而runnable是接口;thread本身是实现了runnable接口的类。我们知道“一个类只能有一个父类,但是却能实现多个接口”,因此runnable具有更好的扩展性。
此外,runnable还可以用于“资源的共享”。即,多个线程都是基于某一个runnable对象建立的,它们会共享runnable对象上的资源。
通常,建议通过“runnable”实现多线程!
thread和runnable的多线程示例
1.thread的多线程示例
下面通过示例更好的理解thread和runnable,借鉴网上一个例子比较具有说服性的例子。// threadtest.java 源码

class mythread extends thread{
  private int ticket=10; 
  public void run(){
    for(int i=0;i<20;i++){ 
      if(this.ticket>0){
        system.out.println(this.getname()+" 卖票:ticket"+this.ticket--);
      }
    }
  } 
};

public class threadtest { 
  public static void main(string[] args) { 
    // 启动3个线程t1,t2,t3;每个线程各卖10张票!
    mythread t1=new mythread();
    mythread t2=new mythread();
    mythread t3=new mythread();
    t1.start();
    t2.start();
    t3.start();
  } 
} 

运行结果:

thread-0 卖票:ticket10
thread-1 卖票:ticket10
thread-2 卖票:ticket10
thread-1 卖票:ticket9
thread-0 卖票:ticket9
thread-1 卖票:ticket8
thread-2 卖票:ticket9
thread-1 卖票:ticket7
thread-0 卖票:ticket8
thread-1 卖票:ticket6
thread-2 卖票:ticket8
thread-1 卖票:ticket5
thread-0 卖票:ticket7
thread-1 卖票:ticket4
thread-2 卖票:ticket7
thread-1 卖票:ticket3
thread-0 卖票:ticket6
thread-1 卖票:ticket2
thread-2 卖票:ticket6
thread-2 卖票:ticket5
thread-2 卖票:ticket4
thread-1 卖票:ticket1
thread-0 卖票:ticket5
thread-2 卖票:ticket3
thread-0 卖票:ticket4
thread-2 卖票:ticket2
thread-0 卖票:ticket3
thread-2 卖票:ticket1
thread-0 卖票:ticket2
thread-0 卖票:ticket1

结果说明:
(1) mythread继承于thread,它是自定义个线程。每个mythread都会卖出10张票。
(2) 主线程main创建并启动3个mythread子线程。每个子线程都各自卖出了10张票。

2.runnable的多线程示例
下面,我们对上面的程序进行修改。通过runnable实现一个接口,从而实现多线程。

// runnabletest.java 源码
class mythread implements runnable{ 
  private int ticket=10; 
  public void run(){
    for(int i=0;i<20;i++){ 
      if(this.ticket>0){
        system.out.println(thread.currentthread().getname()+" 卖票:ticket"+this.ticket--);
      }
    }
  } 
}; 

public class runnabletest { 
  public static void main(string[] args) { 
    mythread mt=new mythread();

    // 启动3个线程t1,t2,t3(它们共用一个runnable对象),这3个线程一共卖10张票!
    thread t1=new thread(mt);
    thread t2=new thread(mt);
    thread t3=new thread(mt);
    t1.start();
    t2.start();
    t3.start();
  } 
}

运行结果:

thread-0 卖票:ticket10
thread-2 卖票:ticket8
thread-1 卖票:ticket9
thread-2 卖票:ticket6
thread-0 卖票:ticket7
thread-2 卖票:ticket4
thread-1 卖票:ticket5
thread-2 卖票:ticket2
thread-0 卖票:ticket3
thread-1 卖票:ticket1

结果说明:
(1) 和上面“mythread继承于thread”不同;这里的mythread实现了thread接口。
(2) 主线程main创建并启动3个子线程,而且这3个子线程都是基于“mt这个runnable对象”而创建的。运行结果是这3个子线程一共卖出了10张票。这说明它们是共享了mythread接口的。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网