java多线程技术

本文介绍Java中创建线程的两种方法:实现Runnable接口和继承Thread类,并详细讲解了如何利用线程池提高效率,包括单一线程、固定大小和缓存线程池的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

进程:进程是计算机上的程序关于某个数据集合上的一次运行活动;每一个进程有一个单独的数据结构:进程控制块(PCB);创建进程实质上就是创建进程映像中的PCB。撤销进程,实质上是撤销进程的PCB。

Java提供两种方法来创建一个线程:

实现Runnable接口:


public class ThreadByImplementRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("A thread by implemnting Runnable Interface");
    }
}

通过实现Runnable接口,最主要的是实现run方法,在其中写入自己的业务逻辑。然后启动一个线程的语句为:


ThreadByImplementRunnable tb = new ThreadByImplementRunnable();
Thread t = new Thread(tb);
/*
这一步很关键,只有调用start方法,才会通过start方法调用系统的方法来真正创建一个线程
*/
t.start(); 

继承Thread:


public class ThreadByExtendThread extends Thread {

    @Override
    public void run() {
        System.out.println("A thread by extending Thread class");
    }
}

Thread这个类本身就是实现Runnable接口,通过继承Thread,来启动一个线程最为简单。代码为:


ThreadByExtendThread t = new ThreadByExtendThread();
t.start();

上面提到的都是最基本的创建一个线程的方法。但是只要接触过线程,基本都会通过线程池的概念。在jdk中,就提供了创建线程池的技术。

在java.util.concurrent这个包下,有一个类Executors,这个类提供了多个创建不同类型线程池的static方法。

newSingleThreadExecutor() :相当于我们平常所说的单例模式,只会同时存在一个线程对象。使用这个方法的测试代码如下:


public class ThreadTest {
    public static void main(String[] args) {
        ThreadForSingleThread t1 = new ThreadForSingleThread();
        ThreadForSingleThread t2 = new ThreadForSingleThread();
        Executor pool1 = Executors.newSingleThreadExecutor();
        pool1.execute(t1);
        pool1.execute(t2);
    }

   private static final class ThreadForSingleThread implements Runnable{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        @Override
        public void run() {
            System.out.println("start time:" + sdf.format(new Date(System.currentTimeMillis())));
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("end time:" +sdf.format(new Date(System.currentTimeMillis())));
        }
    }
}

输出为:

start time:2017-05-29 13:53:40
end time:2017-05-29 13:53:45
start time:2017-05-29 13:53:45
end time:2017-05-29 13:53:50

根据结果集分析,在上一个线程执行完毕之前,下一个线程并没有任何输出,因此可以推测在运行期间,仅仅创建了一个线程。具体代码请查看源代码。

newFixedThreadPool(int nThreads):创建一个可以同时存在nThreads个数量的线程池。创建线程的方式如下:


Thread t1 = new Thread();
Thread t2 = new Thread();
Thread t3 = new Thread();
Executor pool = Executors.newFixedThreadPool(3);
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);

newFixedThreadPool这个方法的测试代码请模仿newSingleThreadExecutor这个方法的例子。

newCachedThreadPool():根据实际的需要动态的改变线程池中的线程对象,数目不固定。创建线程的如下:


Thread t1 = new Thread();
Thread t2 = new Thread();
Thread t3 = new Thread();
Executor pool = Executors.newCachedThreadPool(3);
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);

测试代码请参照newSingleThreadExecutor

总结:上述所提到到的都是最基本的使用线程和线程池的例子。如果想深入了解线程池机制,可以读jdk中的源码。最好的方法感觉还是用到什么再查什么,如果直接读源码,会很累的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值