Java多线程(1)——基本实现方式

在整理的时候不小心把这篇帖子的内容给删了,我记得这时写的挺不错的,悲痛中重新写过。——2017/1/12

Java线程大体上分为三种实现方式,继承Thread类方法,实现Runnable接口方法,线程池方法。

继承Thread类方法

1,创建继承Thread类的子类;
2,在子类中重写Thread的run()方法;
3,调用类的实例的start()方法。

package com.way.threads;

/**
 * 
 * @author reus
 * 多线程实现方法1——继承Thread
  1,创建继承Thread类的子类;
  2,在子类中重写Thread的run()方法
  3,调用类的实例的start()方法
 */
public class ThreadHolder extends Thread{
    //1,创建继承Thread类的子类;
    public ThreadHolder(String name){
        this.setName(name);
    }
    //2,在子类中重写Thread的run()方法
    public void run(){
        for(int i=0;i<100;i++){
            System.out.println(this.getName().toString()+"第"+i+"次抢到线程");
        }

    }

    public static void main(String[] args){
        //3,调用类的实例的start()方法
        ThreadHolder th1=new ThreadHolder("th1");
        ThreadHolder th2=new ThreadHolder("th2");
        th1.start();
        th2.start();        
    }   
}

部分打印结果:

th1第51次抢到线程
th1第52次抢到线程
th1第53次抢到线程
th2第45次抢到线程
th1第54次抢到线程
th1第55次抢到线程
th1第56次抢到线程
th2第46次抢到线程
th1第57次抢到线程
th2第47次抢到线程
th1第58次抢到线程
th2第48次抢到线程
th1第59次抢到线程
th2第49次抢到线程
th1第60次抢到线程
th1第61次抢到线程

实现Runnable接口

静态代理模式
1,创建实现Runnable接口的类,重写run方法;
2,创建真实对象,即实例化类;
3,创建代理:Thread对象,并引用是指真实对象;
4,代理.start()。

/**
 * 
 * @author reus
 * Java多线程实现方式:实现Runnable接口
 *1,创建实现Runnable接口的类,重写run方法
 *2,创建真实对象,即实例化类
 *3,创建代理:Thread对象,并引用是指真实对象
 *4,代理.start();
 */
//1,创建实现Runnable接口的类
public class RunnableHolder implements Runnable{
    String name;

    public RunnableHolder(String name){
        this.name=name;
    }
    //重写run方法
    public void run() {
        for(int i=0;i<100;i++){
            System.out.println(name+"第"+i+"次抢到线程");
        }
    }

    public static void main(String[] args){
        //创建真实对象,即实例化类
        RunnableHolder rh1=new RunnableHolder("rh1");
        RunnableHolder rh2=new RunnableHolder("rh2");
        //创建代理:Thread对象,并引用是指真实对象
        Thread thread1=new Thread(rh1);
        Thread thread2=new Thread(rh2);
        //代理.start();
        thread1.start();
        thread2.start();
    }

}

Callable方法

1,创建实现Callable接口的类,重写call方法;
2,创建线程管理器ExcutorService,创建线程;
3,实例化实现Callable接口的类
4,ExcutorService.submit(类实例);
(5,Callable有返回值,Future result=ser.submit(类实例);)

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 
 * @author reus
 * Java多线程实现方法:Callable接口实现
 *1,创建实现Callable接口的类,重写call方法;
 *2,创建线程管理器ExcutorService,创建线程;
 *3,实例化实现Callable接口的类
 *4,ExcutorService.submit(类实例);
 *(5,Callable有返回值,Future result=ser.submit(类实例);)
 */


//1,创建实现Callable接口的类,重写call方法;
public class CallableHolder implements Callable<Integer>{

    String name;
    int id;

    public CallableHolder(String name,int id) {
        this.name=name;
        this.id=id;
    }

    @Override
    public Integer call() throws Exception {
        for(int i=0;i<100;i++){
            System.out.println(name+"第"+i+"次抢到线程");
        }
        return id;
    }


    public static void main(String[] args){
        //2,创建线程管理器ExcutorService,创建线程;
        ExecutorService es=Executors.newFixedThreadPool(2);
        //3,实例化实现Callable接口的类
        CallableHolder ch1=new CallableHolder("ch1", 1);
        CallableHolder ch2=new CallableHolder("ch2", 2);
        //4,ExcutorService.submit(类实例);
        Future result1=es.submit(ch1);
        Future result2=es.submit(ch2);

        try {
            //(5,Callable有返回值,Future result=ser.submit(类实例);)
            int id1=(int) result1.get();
            int id2=(int) result2.get();
            System.out.println(id1);
            System.out.println(id2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

}

Callable接口定义的只有call一个方法:

public interface Callable<V> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    V call() throws Exception;
}

Callable的功能类似于Runnable,所不同的是Callable具有返回值并且可以抛出异常。

Callable复杂也强大的多,它是JDK1.5推出的Java并发机制的组成部分,关于Java并发,是非常重要的内容,详情应该阅读java.util.concurrent的源码。

参考资料:
http://blog.csdn.net/evankaka
http://blog.csdn.net/escaflone/article/details/10418651
http://blog.csdn.net/picway/article/details/52789895
http://blog.csdn.net/aboy123/article/details/38307539/
北京尚学堂Java教学视频
《Java编程思想》

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值