JAVA 线程实现/创建的方式都有哪些?

线程

线程,又是轻量级进程。程序中的一个顺序控制流程,同时也是CPU的基本调度单位。进程由多个线程组成,彼此间完成不同的工作,交替运行,成为多线程

在这里插入图片描述

创建的线程方式

~第一种,继承Thread

而Thread 类本质上是实现了 Runnable 接口的一个实例,代表一个线程的实例,重写run方法,完成任务

public class ThreadTest {
    
    
	public static void  main(String[] args) {
    
    
		
		MyThread mt = new MyThread();
		MyThread2 mt2 = new MyThread2();
		
		//开启线程
		mt.start();
		mt2.start();
		
		System.out.println("程序结束");
	}
}

//线程1继承Thread
class MyThread extends Thread{
    
    
	public void run() {
    
    
		for(int i=0 ;i<50;i++) {
    
    
			System.out.println("myThread:"+i);
		}
	}
}
//线程2继承Thread
class MyThread2 extends Thread{
    
    
	public void run() {
    
    
		for(int i=0;i<50;i++) {
    
    
			System.out.println("myThread2:"+i);
		}
	}
}

~第二种方式,实现Runnable接口

注:如果自己的类已经 extends 另一个类,就无法直接 extends Thread,此时,可以实现一个 Runnable 接口

package ThreadMost;

public class RunnableTest {
    
    
	public static void main(String[] args) {
    
    
		
		//创建实现类对象
		MyRunnable mr = new MyRunnable();
		MyRunnable2 mr2 = new MyRunnable2();
		
		//将任务交给线程
		Thread t1 = new Thread(mr);
		Thread t2 = new Thread(mr2);
		//开启线程
		t1.start();
		t2.start();
		
		System.out.println("程序结束");
	}
}

//线程1实现Runnable接口
class MyRunnable implements Runnable{
    
    
	@Override
	public void run() {
    
    
		// TODO Auto-generated method stub
		for(int i =1;i<50;i++) {
    
    
			System.out.println("MyRunnable:"+i);
		}
	}
}
//线程2实现Runnable接口
class MyRunnable2 implements Runnable{
    
    
	@Override
	public void run() {
    
    
		// TODO Auto-generated method stub
		for(int i =1;i<50;i++) {
    
    
			System.out.println("MyRunnable2:"+i);
		}
	}
}

~第三种方式,实现Callable接口

具有线程返回值,可以声明异常

注:有返回值的任务必须实现 Callable 接口,类似的,无返回值的任务必须 Runnable 接口

public class CallableTest {
    
    
	public static void main(String[] args) throws InterruptedException, ExecutionException {
    
    
		
		//创建一个线程池
		ExecutorService es = Executors.newFixedThreadPool(3);
		
		Callable<Integer> cb1 = new CallTest();
		Callable<Integer> cb2 = new CallTest();

		// 执行任务并获取 Future 对象
		Future<Integer> s1 = es.submit(cb1);
		Future<Integer> s2 = es.submit(cb2);

		// 关闭线程池
		pool.shutdown();
		
		// 获取所有并发任务的运行结果
		Integer i1 = s1.get();
		Integer i2 = s1.get();
		System.out.println(i1+"\t"+i2);
	}
}

class CallTest implements Callable<Integer>{
    
    
	
	//实现接口重写方法
	@Override
	public Integer call() throws Exception {
    
    
		// TODO Auto-generated method stub
		int sum = 0;
		for(int i=0;i<100;i++) {
    
    
			System.out.println(Thread.currentThread().getName()+":"+i);
			sum +=i;
		}
		return sum;
	}
}
  • Future接口
    异步接收ExecutorService.submit()所返回的状态结果,当中包含了call()的返回值。
    以.get()阻塞方式等待Future中的异步处理结果

~第四种方式,基于线程池的方式

线程和数据库连接这些资源都是非常宝贵的资源。那么每次需要的时候创建,不需要的时候销
毁,是非常浪费资源的。那么我们就可以使用缓存的策略,也就是使用线程池。

public class RunnableTest {
    
    
	public static void main(String[] args) {
    
    
		
		// 创建线程池
		ExecutorService threadPool = Executors.newFixedThreadPool(10);
	 	while(true) {
    
    
	 		threadPool.execute(new Runnable() {
    
     // 提交多个线程任务,并执行
		 		@Override
		 		public void run() {
    
    
			 		System.out.println(Thread.currentThread().getName() + " is running ..");
			 		try {
    
    
			 			Thread.sleep(3000);
			 		} catch (InterruptedException e) {
    
    
			 			e.printStackTrace();
			 		}
		 		}
	 		});
	 	}
	}
}

希望可以帮助到您

~感谢您的光临~

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_50762431/article/details/129992423