实现java多线程的三种方式

最近接触了异步线程,才知道java有实现多线程的第三种方式。

一.继承Thread类创建线程类
重写父类run()方法

      public class thread1 extends Thread {  
       public void run() {  
           for (int i = 0; i < 10000; i++) {  
               System.out.println("线程一"+i);  
           }  
       }  

      public static void main(String[] args) {  
           thread1 th1 = new thread1();  
           thread1 th2 = new thread1();  
           th1.run();  
           th2.run();  
       }  
    }    

run( )方法只是普通的方法,是顺序执行的,虽然我们是写了一个线程,但是并没有体现出多线程的意义。为了体现多线程,应该使用start( )方法来启动线程,start()方法会自动调用run( )方法。所以上面的代码改为:

    public class thread1 extends Thread {    
        public void run() {  
            for (int i = 0; i < 10000; i++) {  
                System.out.println("线程一"+i);  
            }  
        }  

    public static void main(String[] args) {  
            thread1 th1 = new thread1();  
            thread1 th2 = new thread1();  
            th1.start();  
            th2.start();  
        }  

    }  

通过start( )方法启动的线程。不管th1.start( )调用的run( )方法是否执行完,都继续执行th2.start( )。如果下面有别的代码也同样不需要等待th2.start( )执行完而继续执行。

二.实现Runnable接口创建线程类

    public class thread2 implements Runnable {  
        public String ThreadName;  

        public thread2(String tName){  
            ThreadName = tName;  
        }  

        public void run() {  
            for (int i = 0; i < 10000; i++) {  
                System.out.println(ThreadName);  
            }  
        }  

        public static void main(String[] args) {  
            thread2 th1 = new thread2("线程A");  
            thread2 th2 = new thread2("线程B");  
            Thread myth1 = new Thread(th1);  
            Thread myth2 = new Thread(th2);  
            myth1.start();  
            myth2.start();  
        }  
    }  

其实Thread类也是实现了Runnable接口,代表了一个线程的实例,并且启动线程唯一的方法就是通过Thread类的start( )方法。所以第二种实现多线程的方法必须要提拔个工艺new一个Thread类去启动线程。同样的,也不能只是运行run( )方法,因为这样只是一个线程。

再举一个共享数据量的例子:

public class SellTicket2 implements Runnable{
    private int Number=100;
    public void run() {
        // TODO Auto-generated method stub

        while(Number>0){
            System.out.println(Thread.currentThread().getName()+" 出售了第["+(Number--)+"]张票");

        }
    }
}
/*
 * 需求:火车站卖票:
 *  共有火车票100张,有三个窗口在卖,请通过代码老模拟该卖票程序
 *  1. 继承Thread类
 *  2. 实现Runnable接口
 */
//方式2. 实现Runnable接口
     //共享数据,三个线程共买100张火车票
public class SellTicketDemo2 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub


        SellTicket2  r2=new SellTicket2();
        Thread   t1=new Thread(r2,"窗口1");
        Thread   t2=new Thread(r2,"窗口2");
        Thread   t3=new Thread(r2,"窗口3");
        t1.start();
        t2.start();
        t3.start();
    }

}

三、使用ExecutorService、Callable、Future实现由返回结果的多线程
这种方式是之前没有尝试过的,ExecutorService是一个线程池接口,可返回值的任务必须实现Callable接口,无返回值得任务必须实现Runnable接口。执行Callable任务后,可以获取一个Future对象,在该对象上调用get就可以获取到Callable任务返回的Object了,再结合ExecutorService接口就可以实现有返回结果的多线程了。

     /** 
     * 有返回值的线程 
     */  
    @SuppressWarnings("unchecked")  
    class Test {  
        public static void main(String[] args) throws ExecutionException,  
                InterruptedException {  
            System.out.println("----程序开始运行----");  
            Date date1 = new Date();  

            int taskSize = 5;  
            // 创建一个线程池  
            ExecutorService pool = Executors.newFixedThreadPool(taskSize);  
            // 创建多个有返回值的任务  
            List<Future> list = new ArrayList<Future>();  
            for (int i = 0; i < taskSize; i++) {  
                Callable c = new MyCallable(i + " ");  
                // 执行任务并获取Future对象  
                Future f = pool.submit(c);  
                // System.out.println(">>>" + f.get().toString());  
                list.add(f);  
            }  
            // 关闭线程池  
            pool.shutdown();  

            // 获取所有并发任务的运行结果  
            for (Future f : list) {  
                // 从Future对象上获取任务的返回值,并输出到控制台  
                System.out.println(">>>" + f.get().toString());  
            }  

            Date date2 = new Date();  
            System.out.println("----程序结束运行----,程序运行时间【"  
                    + (date2.getTime() - date1.getTime()) + "毫秒】");  
        }  
    }  

    class MyCallable implements Callable<Object> {  
        private String taskNum;  

        MyCallable(String taskNum) {  
            this.taskNum = taskNum;  
        }  

        public Object call() throws Exception {  
            System.out.println(">>>" + taskNum + "任务启动");  
            Date dateTmp1 = new Date();  
            Thread.sleep(1000);  
            Date dateTmp2 = new Date();  
            long time = dateTmp2.getTime() - dateTmp1.getTime();  
            System.out.println(">>>" + taskNum + "任务终止");  
            return taskNum + "任务返回运行结果,当前任务时间【" + time + "毫秒】";  
        }  
    }     

返回结果:
这里写图片描述

总结
实现多线程的几种方式,建议使用runable实现,不管如何最终都需要thread.start( )来启动线程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值