Java 多线程的创建与使用

介绍

  当java程序执行main方法的时候,就是在执行一个名字叫做main的线程。可以在main方法执行时,开启多个线程A、B、C。

  Thread类是java.lang包下的一个常用类,每一个Thread类的对象,就代表一个处于某种状态的线程。

Thread 类一些方法:

public final String getName() 获取线程名称
public final void setName(String name) 指定线程名称
public static Thread currentThread() 获取当前线程对象

实现

  1、继承Thread类

public class MyThread extends Thread{
	@Override
	public void run() {
		for(int i=0;i<50;i++){
			//System.out.println("MyThread:"+i);
			System.out.println(this.getName()+":"+i);
		}
	}	
}
public class ThreadDemo {
	public static void main(String[] args) {	
		//开启线程
		//创建线程对象
		MyThread thread = new MyThread();	
		//开启线程动作
		thread.start();		
	}
}
//使用匿名内部类创建线程的子类对象
Thread thread = new Thread(){
	@Override
	public void run() {
		System.out.println("我的线程执行了");
	}	
};
//开启线程
thread.start();

//使用匿名内部类创建线程的子类匿名对象
new Thread(){
	@Override
	public void run() {
		System.out.println("我的线程执行了");
	}
	
}.start();

  2、实现Runable接口

public class MyRunnable implements Runnable{
	@Override
	public void run() {		
		//返回当前线程
		Thread thisThread = Thread.currentThread();
		for(int i=0;i<10;i++){
			//System.out.println("线程名称:"+i);
			System.out.println(thisThread.getName()+":"+i);
		}
	}
}
public class ThreadDemo2 {
	public static void main(String[] args) {	
		//开启线程
		//创建线程执行目标
		MyRunnable mr = new MyRunnable();
		//通过指定线程执行目标的构造方法创建线程对象
		Thread thread = new Thread(mr);
		thread.setName("Thread1");	
		//开启线程动作
		thread.start();
			
		//返回main线程
		Thread mainThread = Thread.currentThread();
		for(int i=0;i<10;i++){
			//System.out.println("main:"+i);
			System.out.println(mainThread.getName()+":"+i);
		}		
	}
}
//使用匿名内部类的方式,创建线程执行目标类的对象
Runnable runnable = new Runnable() {
	@Override
	public void run() {
		System.out.println("我的线程执行目标,执行了");
	}
};

//通过目标创建线程对象
Thread thread2 = new Thread(runnable);
//开启线程
thread2.start();

//使用匿名内部类的方式,创建线程执行目标类的匿名对象
//通过目标创建线程对象
Thread thread3 = new Thread(new Runnable() {
	@Override
	public void run() {
		System.out.println("我的线程执行目标,执行了");		
	}
});
//开启线程

线程池

  Executors:线程池创建工厂类

public static ExecutorService newFixedThreadPool(int nThreads): 返回线程池对象

  ExecutorService:线程池类

Future<?> submit(Runnable task):
获取线程池中的某一个线程对象,并执行

  Future接口:用来记录线程任务执行完毕后产生的结果。

  使用:
    1、利用Runnable提交任务

public class MyRunnable implements Runnable{
	@Override
	public void run() {		
		//返回当前线程
		Thread thisThread = Thread.currentThread();
		for(int i=0;i<10;i++){
			//System.out.println("线程名称:"+i);
			System.out.println(thisThread.getName()+":"+i);
		}
	}
}

public class ThreadPoolDemo {
	public static void main(String[] args) {
		//返回一个线程池
		ExecutorService threadPool = Executors.newFixedThreadPool(3);
		
		//创建线程执行目标
		MyRunnable myRunnable = new MyRunnable();
		
		//向线程池提交任务
		threadPool.submit(myRunnable);
		
		//在适当的时候,关闭线程池
		threadPool.shutdown();
	}
}

    2、利用Callable提交任务
      Callable接口:

与Runnable接口功能相似,用来指定线程的任务。其中的call()方法,用来返回线程任务执行完毕后的结果,call方法可抛出异常。

      ExecutorService:线程池类

<T> Future<T> submit(Callable<T> task):
获取线程池中的某一个线程对象,并执行线程中的call()方法

public class MyCallable implements Callable<String>{
	@Override
	public String call() throws Exception {
		return "线程任务的返回值";
	}
}
public class ThreadPoolDemo2 {
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		//返回一个线程池
		ExecutorService threadPool = Executors.newFixedThreadPool(3);
			
		//创建带返回值的线程执行目标
		MyCallable myCallable = new MyCallable();
		
		//向线程池提交任务,并返回线程执行目标的结果
		Future<String> future = threadPool.submit(callable);
		
		//从执行结果中返回call的具体返回值
		String result = future.get();
		System.out.println(result);
		
		//在适当的时候,关闭线程池
		threadPool.shutdown();
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值