Java多线程创建方式

一、多线程的三种创建方式

   Java多线程有三种创建方式,实际上可以分为两种,一种方法是继承Thread类,一种是实现Runnable接口或者Callable接口。

1.继承Thread类创建线程

继承Thread类,重写run方法,run方法的方法体就是线程执行体

package com.langsin.thread1;
/*
 * 继承方式创建多线程
 * 直接使用this即可获得当前线程
 * 单继承局限性
 * 多个线程不能共享实例变量
 */
public class MyFirstThread extends Thread{
    int i=0;

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(this.getName()+":"+i);
        }
    }
    public static void main(String[] args) {
        MyFirstThread thread1=new MyFirstThread();
        MyFirstThread thread2=new MyFirstThread();
        thread1.start();//start方法开启线程,会自动调用run方法,执行线程体。但不能直接调用run方法,否则就成为普通单线程程序
        thread2.start();

    }
}

运行结果如下:

Thread-0:0
Thread-1:0
Thread-0:1
Thread-0:2
Thread-1:1
Thread-0:3
Thread-1:2
Thread-0:4
Thread-0:5
Thread-0:6
Thread-0:7
Thread-1:3
Thread-0:8
Thread-0:9
Thread-1:4
Thread-1:5
Thread-1:6
Thread-1:7
Thread-1:8
Thread-1:9

每次运行结果不尽相同,谁先抢占到CPU,谁先执行,因此每次运行打印顺序有差异。

此结果共打印20次而不是两个线程共打印10次,因为new了两个独立的对象


2.实现Runnable接口创建线程

重写run方法,同样run方法的方法体就是线程执行体

以Runnable实现类的实例为target创建Thread对象

/*
 * 必须用Thread.currentThread()才能获得当前线程
 * 实现Runnable接口,还可以继承其他类
 * 方便多线程间共享实例变量
 *
 */
public class MyFirstThread2 implements Runnable{
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for (int i=0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
    public static void main(String[] args) {
        
        MyFirstThread2 m=new MyFirstThread2();
        for (int i=0; i < 20; i++) {
            System.out.println(Thread.currentThread().getName()+":"+i);
            if(i==5){
                Thread t1=new Thread(m,"t1");//Runnable对象m只是作为线程对象的target,多个线程可以共享同一个target
                t1.start();
            }
            if(i==10){
                Thread t2=new Thread(m,"t2");
                t2.start();

            }
            
        }
    }

运行结果如下:


main:0
main:1
main:2
main:3
main:4
main:5
main:6
main:7
t1:0
main:8
t1:1
main:9
t1:2
main:10
t1:3
t1:4
t1:5
t1:6
t1:7
t1:8
t1:9
main:11
t2:0
main:12
t2:1
main:13
t2:2
main:14
t2:3
main:15
t2:4
main:16
t2:5
main:17
t2:6
main:18
t2:7
main:19
t2:8
t2:9

大家可能从结果中看到,并不是i==5时,接下来立即运行线程t1,这是因为虽然i==5时开启了线程t1,但在与线程main抢占CPU时,线程main先抢到了,t2情况与此相同。

我们可以在接下来的学习中学到线程的优先级问题,可以人为的干预线程的优先运行情况,可以姑且把优先级理解为抢占CPU的能力。

3.实现Callable接口创建线程

重写call方法,与2的run方法相比,该方法可以有返回值,并且可由声明抛出异常。

Callable接口不是Runnable接口的子接口,不能直接作为Thread对象的target,并且call方法有返回值,因此需要一种获取返回值的办法。Java 5提供Futrue接口来代表call方法的返回值。Future接口有一个FutureTask实现类,该类同时实现了Runnble接口,因此可以作为Thread对象的target,所以我们可用FutureTask类包装Runnble对象,把FutureTask对象作为target传给Thread对象并开启线程。FutureTask对象的get方法可以获取call方法的返回值。


package com.langsin.thread1;

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

public class MyFirstThread3 {
    public static void main(String[] args) throws InterruptedException,
            ExecutionException {

        /*Lambda表达式方式

         * FutureTask<Integer> f=new  FutureTask<Integer>((Callable<Integer>)()->

         *{ int i=0; for (; i < 100; i++)
         * { System.out.println(Thread.currentThread().getName()+":"+i); }
         * return i; });
         */

       //匿名内部类方式
        FutureTask<Integer> f = new FutureTask<Integer>(//一旦运行就执行给定的Callable
                new Callable<Integer>()
{

                    @Override
                    public Integer call() throws Exception {
                        // TODO Auto-generated method stub
                        int i = 0;
                        for (; i < 100; i++) {
                            System.out.println(Thread.currentThread().getName()  + ":" + i);
                                 
                        }

                        return i;
                    }

                });

        new Thread(f, "callableThread1").start();
        System.out.println(f.get());//打印call方法返回值


    }
}

运行结果如下:

callableThread1:0
callableThread1:1
callableThread1:2
callableThread1:3
callableThread1:4

......

callableThread1:96
callableThread1:97
callableThread1:98
callableThread1:99
100



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值