线程

intro

在Java中,程序的单位是类,但在操作层面而言,程序的单位是函数。函数决定着数据的控制、处理。而线程,就是一个控制函数运行的管家,在并行计算中,多线程可以加速程序的运行。

构造线程的方法

出乎意料,构造程序所需的东西都在java.lang包内。所以你无需import任何东西就可以构造多线程了。方法有三:

  • 通过实现 Runnable 接口;
  • 通过继承 Thread 类本身;
  • 通过 Callable 和 Future 创建线程。

熟悉C#的朋友就会发现它们在构造多线方面简直一摸一样。

通过Thread类

Thread自带run和start

// ThreadDemo.java
class ThreadDemo extends Thread{
	private Thread t;
	private String threadName;
	
	ThreadDemo(String name){
		threadName = name;
		System.out.println("Creating " + threadname);
	}
	
	public void run(){
		System.out.println("Running " + threadName);
		try{
			for(int i = 4; i > 0 ; i--) {
				System.out.println("Thread: " + threadName + ", " + i);
				t.sleep(50);
			}
		}catch(InterruptedException e) {
			System.out.println("Thread " + thraedName + "interrupted.");
		}
		System.out.print;("Thread " + threadName + " exiting");
	}

	public void start() {
		System.out.println("Starting " + threadName);
		if(t == null) {
			t = new Thread(this, threadName);
			t.start();
		}
	}
}
// TestThread.java
public class TestThread{
	public static void main(String[] arg[]) {
		ThreadDemo T1 = new ThreadDemo("Thread-1");
		T1.start();
		ThreadDemo T2 = new
		T2.start();
	}
}

通过Runnable接口

遇到新的类或接口时,先查jdk或在eclipse点进去看一下。

package java.lang;
@FunctionalInterface
public interface Runnable{
	public abstract void run();
}
  • 可见,Runnable接口只有一个抽象函数,原来接口也可以定义抽象函数的。注意Java的抽象函数和C++的纯虚函数一样,在子类必须要实现。
  • @FunctionalInterface是特性,在Java叫函数式接口,也就是Single Abstract Method interfaces。可看我的另一篇博文@FunctionalInterface
  • 由于Runnable只有run方法,我们还是需要用Thread类来启动线程。
// RunnableDemo.java
class RunnabelDemo implements Runnable{
	private Thread t;
	private String threadName;
	
	RunnableDemo(String name){
		threadName = name;
		System.out.println("Creating " + threadName);
	}
	
	public void run() {
		System.out.println("Runnning " + threadName);
		try{
			for(int i = 4; i > 0; i--){
				System.out.println("Tread: " + threadName + "," + i);
				Thread.sleep(50); //50ms
			}
		}catch(InterruptedException e){
			System.out.println("Thread " + threadName + "interrupted.");
		}
		System.out.prinln("Thread " + threadName + " exiting");
	}

	public void start(){
		System.out.println("Starting " + threadName);
		if(t == null) {
			t = new Thread(this, threadname);
			t.start();
		}
	}
}
// TestThread.java
public class TestThread{
	public static void main(String[] args){
		RunnableDemo R1 = new Runnable("Thread-1");
		R1.start();
		RunnableDemo R2 = new Runnable("Thread-2");
		R2.start();
	}
}

继承Runnable之后还要嵌套一个Thread类成员,不懂这样有啥优势?注意,这里RunnableDemo继承Runnable接口,是没有start函数的。

通过 Callable 和 FutureTask 创建线程

又是不认识的接口和类,先看下jdk和源码

// java.util.concurrent.Callable
@FunctionalInterface
public interface Callable<V> {
	V call() throws Exception;
]

原来throws还可以出现在函数体内的。Callable是个泛型接口,提供一个call函数。这个call函数能被FutureTask<>作为参数构建,而FutureTask能作为Thread参数构建,是不是绕晕了呢?
先看下FutureTask的类图吧。红框中明确说明了Callable<>可以作为参数进行构造。这也是为什么我们需要在类中继承Callable接口,这样我们的类就可以喂入FutureTask里了,然后再喂入Thread。而泛型参数就是返回类型了。
在这里插入图片描述

public class CallableThreadTest implements Callable<Integer> {
	public static void main(String[] args){
		CallableThreadTest ctt = new CallableThreadTest();
		//Callable喂入FutureTask
		FutureTask<Integer> ft = new FutureTask<>(ctt);
		for(int i = 4; i < 100; i++) {
			System.out.println(Thread.currentThread().getName()+" element "i);
			if(i==20){
				// FutureTask喂入Thread
				new Thread(ft,"Thead with reutrn").start();
			}
		}
		try{
			System.out.println("the reutrn is " + ft.get());
		}catch(InterruptedException e){
			e.printStackTrace();
		}catch(ExecutionException e){
			e.printStackTrace();
		}
	}
	@Override
	public Integer call() throws Exception{
		for(int i = 0;i<100; i++){
			System.out.println(Thread.currentThread().getname()+" " +i);
		}
		return i;
	}
}

我们可以通过thread.get()获得call函数的返回值哦~


Reference

[1] https://www.runoob.com/java/java-multithreading.html
[2] https://www.jianshu.com/p/32f89b3e13cc

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值