本文章为个人笔记积累
多线程的四种创建方法
方式一:通过继承Thread类创建
/**
* 多线程的创建,方式一:继承Thread类
* 1.创建一个继承与Thread类的子类
* 2.重写Thread类的run()方法
* 3.创建Thread类的子类对象
* 4.通过创建的子类对象调用start()方法
*/
//主线程
public class ThreadTest {
public static void main(String[] args) {
//3.创建Thread类的子类对象
MyThread myThread1 = new MyThread();
//4.通过创建的子类对象调用start()方法
myThread1.start();
//myThread.run();
//不能通过直接调用run()的方式启动线程
//myThread.start();
//不可以还让已经start()的线程去执行,会报异常IllegalThreadStateException
//需要重新创建一个线程的对象
MyThread myThread2 = new MyThread();
myThread2.start();
}
}
//子线程
//1.创建一个继承与Thread类的子类
class MyThread extends Thread{
//2.重写Thread类的run()方法
@Override
public void run() {
//在此线程执行的操作声明在run()方法中
//遍历100以内的偶数
for (int i = 0; i < 100; i++) {
if (i % 2 == 0){
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
}
方式二:通过实现Runnable接口创建
/**
* 创造多线程方式二:实现Runnable接口
* 1.创建一个实现Runnable接口的类
* 2.实现类去实现Runnable中的抽象方法:run()
* 3.创建实现类的对象
* 4.将此对象作为参数传递到Thread类中的构造器中,创建Thread类的对象
* 5.通过Thread类的对象调用start():①启动线程 ②调用当前线程的run()-->调用了Runnable类型的target的run()
*/
//1.创建一个实现Runnable接口的类
class MyThread implements Runnable{
//2.实现类去实现Runnable中的抽象方法:run()
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (i % 2 == 0){
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
}
public class ThreadTest1 {
public static void main(String[] args) {
//3.创建实现类的对象
MyThread myThread = new MyThread();
//4.将此对象作为参数传递到Thread类中的构造器中,创建Thread类的对象
Thread t1 = new Thread(myThread);
//5.通过Thread类的对象调用start():①启动线程 ②调用当前线程的run()-->调用了Runnable类型的target的run()
t1.start();
//再启动一个线程
Thread t2 = new Thread(myThread);
t2.start();
}
}
方式三:实现Callable接口
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* 创建线程方式三:实现Callable接口 ----jdk5.0新增
*
* 如何理解实现Callable接口的方式创建多线程比实现Runnable接口创建多线程方式强大
* 1.call()可以有返回值
* 2.call()可以抛出异常,被外面的操作捕获,获取异常信息
* 3.Callable是支持泛型的
*/
//1.创建实现Callable的实现类
class NumThread implements Callable{
//2.重写call方法,将此线程执行的操作声明在其中
@Override
public Object call() throws Exception {
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0){
System.out.println(i);
sum += i;
}
}
return sum;
}
}
public class ThreadNew {
public static void main(String[] args) {
//3.创建Callable接口实现类的对象
NumThread numThread = new NumThread();
//4.将Callable接口实现类的对象作为参数传递到FutureTask构造器中,创建FutureTask的对象0
FutureTask futureTask = new FutureTask(numThread);
//5.将FutureTak的对象作为参数传递到Thread类的构造器中,创建Thread对象,并调用start()
new Thread(futureTask).start();
try {
//6.获取Callable中call方法的返回值
//get()返回值为Callable实现类中重写call()中的返回值
Object sum = futureTask.get();
System.out.println("总和为:" + sum);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
方式四:使用线程池
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 创建线程的方式四:使用线程池
* 好处:
* 1.提高响应速度(减少了创建新线程的时间)
* 2.降低资源消耗(重复利用线程池中线程,不需要每次都创建)
* 3.便于线程管理
* corePoolSize:核心池的大小
* maximumPoolSize:最大线程数
* keepAliveTime:线程没有任务时最多保持多长时间后会终止
*
* 创建多线程有四种方式
*/
class NumberThread implements Runnable{
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
if (i % 2 == 0){
System.out.println(Thread.currentThread().getName()+ ":" +i);
}
}
}
}
class NumberThread1 implements Runnable{
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
if (i % 2 != 0){
System.out.println(Thread.currentThread().getName()+ ":" +i);
}
}
}
}
public class ThreadPool {
public static void main(String[] args) {
//1.提供指定线程数量的线程池
ExecutorService service = Executors.newFixedThreadPool(10);
//2.执行指定的线程的操作,需要提供实现Runnable接口或Callable接口实现类的对象
service.execute(new NumberThread()); //适合使用于Runnable
service.execute(new NumberThread1());
//service.submit(Callable callable); //适合使用于Callable
//关闭连接池
service.shutdown();
}
}