多线程基本实现方法(一)

多线程的实现方法主要有三种分别是Thread类,Runnable接口和 callable接口,下面分别来说述一下三者的不一样。java

首先是Thread类,须要被继承以后才能实现,在实现的时候须要覆写run()方法,可是在启动的时候要用start()方法启动。小程序

下面是一个卖票的小程序多线程

 class MyThread extends Thread { //这是一个多线程的操做类
	private int ticket = 10 ;	

	@Override
	public void run(){	//覆写run()方法,做为线程主体操做方法
		for(int x = 0 ;x <100; x ++) {
		if(this.ticket > 0){
		System.out.println("卖票,ticket =" + this.ticket --) ;
}
}
}
}
public class TestDemo {  //主类
	public static void main(String [] args){
	//因为MyThread类有start()方法,因此每个MyThread类对象就是一个线程对象,能够直接启动
	MyThread mt1 = new MyThread() ;
	MyThread mt2 = new MyThread() ;
	MyThread mt3 = new MyThread() ;
	mt1.start();
	mt2.start();
	mt3.start();
}
}

 运行结果ide

Thread类实线多线程以后没有共享资源,以卖票为例,总共十张票,让三我的去卖,若是每一个人都当本身有十张票,那么总共就须要三十张票,这显然不是咱们想要的结果。this

下面以Runnable接口来实现这个程序spa

 class MyThread implements Runnable { //这是一个多线程的操做类
    private int ticket = 10 ;    

    @Override
    public void run(){    //覆写run()方法,做为线程主体操做方法
        for(int x = 0 ;x <100; x ++) {
        if(this.ticket > 0){
        System.out.println("卖票,ticket =" + this.ticket --) ;
}
}
}
}
public class TestDemo {  //主类
    public static void main(String [] args){
    //因为MyThread类有start()方法,因此每个MyThread类对象就是一个线程对象,能够直接启动
    MyThread mt = new MyThread();
    new Thread(mt).start();
    new Thread(mt).start();
    new Thread(mt).start();
}
}

运行结果线程

 

Runnable接口实现多线程就能达到资源共享的状况,同时使用Runnable接口还能避免单继承的局限性。在Thread类中是继承了Runnable接口,因此Runnable的多线程的实现,也是用到了Thread类中的start()方法。设计

一样callable接口是为了在java 1.5以后出现的,它是为了解决Runnable接口操做完以后不能返回操做结果的缺陷而设计的code

import java.util.concurrent.Callable ;
import java.util.concurrent.FutureTask ;

 class MyThread implements Callable<String> { //这是一个多线程的操做类
    private int ticket = 10 ;    
    @Override
    public String call() throws Exception {    
        for(int x = 0 ;x <100; x ++) {
        if(this.ticket > 0){
        System.out.println("卖票,ticket =" + this.ticket --) ;
}
}
    return "票已卖光" ;
}
}
public class TestDemo {  //主类
    public static void main(String [] args)  throws Exception {
    MyThread mt1 = new MyThread();
    MyThread mt2 = new MyThread();
    FutureTask<String> task1 = new FutureTask(mt1) ;//目的是为了取回call()返回结果
    FutureTask<String> task2 = new FutureTask(mt2) ;//目的是为了取回call()返回结果
    //FutureTask是Runnable接口子类,因此可使用Thread类的构造来接收task对象
    new Thread(task1).start();
    new Thread(task2).start();
    //多线程执行完毕后能够取得内容,依靠FutureTask的父接口Future中的get()方法完成
    System.out.println("A线程返回的结果:" + task1.get());
    System.out.println("B线程返回的结果:" + task1.get());
}
}

因为Callable接口实现多线程的方式比较麻烦,因此通常不用此操做,只作了解。对象

相关文章
相关标签/搜索