Java线程的创建

很多书上介绍Java的线程创建有两种方式,一种是继承Thread类,一种是实现Runnable接口。实际上Runnable接口只有一个run方法,Thread也是实现了Runnable接口的,但是无论哪种方式都必须借助Thread类,下面列举了创建线程的两种方式:

->方式一:继承Thread类

public class ChildThread extends Thread {

	/**
	 * 覆盖run方法,编写线程执行的核心代码
	 */
	@Override
	public void run() {
		System.out.println("线程" + Thread.currentThread().getName() + "执行了...");
	}
	
	public static void main(String[] args) {
		//启动线程
		new ChildThread().start();
	}
	
}

->方式二:实现Runnable接口,将Runnable对象作为Thread的目标对象(targetObject)

public class ThreadTargetObject implements Runnable {

	/**
	 * 实现run方法,编写线程执行的核心代码
	 */
	@Override
	public void run() {
		System.out.println("线程" + Thread.currentThread().getName() + "执行了...");
	}

	public static void main(String[] args) {
		//启动线程
		new Thread(new ThreadTargetObject()).start();
	}
}

  注意:必须通过start方法来启动线程,直接调用run方法是无法异步执行线程的,因为start方法会真正调用native方法创 建线程,并调用run方法。

 

当然,以上方法都是我们自己通过new关键字来创建的线程对象,很多时候我们在编程多线程程序的时候会用到线程池,因为创建过多的线程会消耗系统很多的资源,而且每次创建和销毁都会占用一些CPU时间,每个线程运行起来也会变慢,线程池会帮助我们创建线程和分配线程,实现线程对象的重用,这样会更高效,JDK5以后加入了并发包,也就是java.lang.concurrent包,我们也没必要自己去写线程池了,下面是线程池的使用代码:

package com.cugxw.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class ThreadPoolTest {
	public static void main(String args[]) throws InterruptedException {
		
		System.out.println("-----------------------测试newFixedThreadPool----------");
		
		// 在线程池中创建3个线程
		ExecutorService execs = Executors.newFixedThreadPool(3);
		// 创建100 个线程目标对象
		for (int index = 0; index < 10; index++) {
			Runnable run = new ThreadTO(index);
			// 执行线程目标对象
			execs.execute(run);
		}
		execs.shutdown();
		
		System.out.println("-----------------------测试newCachedThreadPool-------");
		
		//使用线程池cache策略
		ExecutorService execsCached = Executors.newCachedThreadPool();
		// 创建10 个线程目标对象
		for (int index = 0; index < 10; index++) {
			Runnable run = new ThreadTO(index);
			// 执行线程目标对象
			execsCached.execute(run);
		}
		execsCached.shutdown();
		
		System.out.println("-----------------------测试newScheduledThreadPool------");
		
		// 在线程池中创建3个线程
		ExecutorService execsSche = Executors.newScheduledThreadPool(3);
		// 创建10 个线程目标对象
		for (int index = 0; index < 10; index++) {
			Runnable run = new ThreadTO(index);
			// 执行线程目标对象
			execsSche.execute(run);
		}
		execsSche.shutdown();
		
		System.out.println("-----------------------测试defaultThreadFactory-------");
		
		//获取一个线程工厂类,用于创建新的线程
		ThreadFactory tf = Executors.defaultThreadFactory();
		tf.newThread(new ThreadTO(100)).start();
		tf.newThread(new ThreadTO(101)).start();
		tf.newThread(new ThreadTO(102)).start();
		tf.newThread(new ThreadTO(103)).start();
		tf.newThread(new ThreadTO(104)).start();
		
	}
}

// 线程目标对象
class ThreadTO implements Runnable {
	int index = 0;

	public ThreadTO(int index) {
		this.index = index;
	}

	@Override
	public void run() {
		System.out.println("线程:" + Thread.currentThread().getName() 
				+ "调用了目标对象" + index);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值