1. 继承Thread类
Thread类在包java.lang中,从这个类中实例化的对象代表线程,启动一个新线程需要建立Thread实例,Thread类中常用的两个构造方法如下:
(1)public Thread(String threadName)
(2)public Thread()
其中第一个构造方法是创建一个名称为threadName的线程对象
创建一个新的线程的语法如下:
public class ThreadTest extends Thread{}
完成线程真正功能的代码方法类run()方法中,当一个类继承Thread类后,就可以在该类中覆盖run方法,将实现该线程功能的代码写入run()方法中国,然后调用Thread类中的start()方法执行线程,即调用run()方法.
Thread对象需要一个任务来执行,任务是指线程在启动时执行的工作,该工作的功能代码被写在run()方法中。当执行一个线程程序时,就自动产生一个线程,主方法是在这个线程上运次的,当不再启动其他线程时,该程序就为单线程程序。
public class Thread1 extends Thread {
Thread1(String name){
super(name);
}
@Override
public void run() {
while (true) {
System.out.println(getName());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Thread1 thread = new Thread1("线程1");
Thread1 thread1 = new Thread1("线程2");
thread1.start();
thread.start();
}
}
2. 实现runnable接口
利用实现Runnable接口来创建线程的方法可以解决JAVA语言不支持多重继承的问题。Thread类拥有众多的方法,而Runnable接口中只有唯一的一个run()方法原型,因此创建新线程类时,只要实现此接口,就可以开始新线程类的运行。
public class Demo2 implements Runnable {
@Override
public void run() {
System.out.println("thread is running");
}
public static void main(String[] args) {
Thread thread = new Thread(new Demo2());
thread.start();
}
}
3. 匿名内部类的方式
public class Demo3 {
public static void main(String[] args) {
new Thread(){
public void run() {
System.out.println("thread1 is running");
};
}.start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread2 is running");
}
}).start();
}
}
4. 带返回值的线程实现
使用ExecutorService、Callable、Future实现有返回结果的多线程
假设有一个很耗时的返回值需要计算,并且这个返回值不是立刻需要的话,那么就可以使用这个组合,用另一个线程去计算返回值,而当前线程在使用这个返回值之前可以做其它的操作,等到需要这个返回值时,再通过Future得到。
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class Demo2 implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println("后台正在紧张计算");
Thread.sleep(2000);
return 3 ;
}
public static void main(String[] args) {
Demo2 demo2 = new Demo2();
FutureTask<Integer> ft = new FutureTask<>(demo2);
new Thread(ft).start();
try {
System.out.println("获取到的计算结果为:"+ft.get());
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("结束计算");
}
}
5.线程池的实现
我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题:
如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁创建线程和销毁线程需要时间。
那么有没有一种办法使得线程可以复用,就是执行完一个任务,并不被销毁,而是可以继续执行其他的任务?
在Java中可以通过线程池来达到这样的效果。
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Demo4 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
System.out.println("----程序开始运行----");
Date date1 = new Date();
int taskSize = 5;
// 创建一个线程池
ExecutorService pool = Executors.newFixedThreadPool(taskSize);
// 创建多个有返回值的任务
List<Future> list = new ArrayList<>();
for (int i = 0; i < taskSize; i++) {
Callable c = new MyCallable(i + "");
// 执行任务并获取Future对象
Future f = pool.submit(c);
list.add(f);
}
// 关闭线程池
pool.shutdown();
// 获取所有并发任务的运行结果
for (Future f : list) {
// 从Future对象上获取任务的返回值,并输出到控制台
System.out.println(">>>" + f.get().toString());
}
Date date2 = new Date();
System.out.println("----程序结束运行----,程序运行时间【"
+ (date2.getTime() - date1.getTime()) + "毫秒】");
}
}
class MyCallable implements Callable<Object> {
private String name;
MyCallable(String name) {
this.name = name;
}
public Object call() throws Exception {
System.out.println("线程:" + name + "开始运行");
long time1 = System.currentTimeMillis();
Thread.sleep(1000);
long time2 = System.currentTimeMillis();
return "线程:" + name + "运行耗时【" + (time2 - time1) + "】";
}
}