三种创建方式:
- 继承Thread类(重点)实现Runnable接口(重点)实现Callable接口(现阶段了解)
自定义线程类继承Thread类重写run()方法,编写线程执行体创建线程对象,调用start()方法启动线程
//自定义线程类继承Thread类
public class TestThread extends Thread{
//重写run()方法,编写线程执行体
@Override
public void run() {
for (int i = 0; i < 200; i++) {
System.out.println("我在学习多线程--"+i);
}
}
public static void main(String[] args) {
//创建线程对象,调用start()方法启动线程
new TestThread().start();
for (int i = 0; i < 200; i++) {
System.out.println("我在吃饭--"+i);
}
}
}
运行结果:
我在吃饭--0 我在学习多线程--0 我在吃饭--1 我在学习多线程--1 我在吃饭--2 我在学习多线程--2 我在吃饭--3 ……
package multithreading;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class TestDownloads extends Thread{
private String url;
private String name;
public TestDownloads(String url,String name){
this.url = url;
this.name = name;
}
public void run(){
PictureDownloader imageDownload = new PictureDownloader();
imageDownload.download(url,name);
}
public static void main(String[] args) {
TestDownloads t1 = new TestDownloads("https://gimg2.baidu.com/image_search/src=https://www.mshxw.com/skin/sinaskin/image/nopic.gif","1.jpg");
TestDownloads t2 = new TestDownloads("https://gimg2.baidu.com/image_search/src=https://www.mshxw.com/skin/sinaskin/image/nopic.gif","2.jpg");
TestDownloads t3 = new TestDownloads("https://gimg2.baidu.com/image_search/src=https://www.mshxw.com/skin/sinaskin/image/nopic.gif","3.jpg");
t1.start();
t2.start();
t3.start();
}
}
// 图片下载器
class PictureDownloader
{
public void download(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
System.out.println("图片下载完成:"+name);
} catch (IOException e) {
System.out.println("IO下载器出现异常,downloadsImage");
}
}
}
执行结果:
图片下载完成:3.jpg 图片下载完成:1.jpg 图片下载完成:2.jpgRunnable
定义MyRunnable类实现Runnable接口实现run()方法,编写线程执行体创建线程对象,调用start()方法启动线程
package multithreading;
//定义MyRunnable类实现Runnable接口
public class TestRunnable implements Runnable{
//实现run()方法,编写线程执行体
@Override
public void run() {
for (int i = 0; i < 200; i++) {
System.out.println("我在学习Runnable接口多线程--"+i);
}
}
public static void main(String[] args) {
//创建Runnable接口的实现类对象
TestRunnable r = new TestRunnable();
//创建线程对象,通过对象来开启线程,代理
Thread thread = new Thread(r);// 里面的参数是Runnable接口的实现类
//这里可以用匿名创建对象的方式
thread.start();
for (int i = 0; i < 200; i++) {
System.out.println("我在吃饭--"+i);
}
}
}
结果:
我在学习Runnable接口多线程--0 我在吃饭--0 我在学习Runnable接口多线程--1 我在吃饭--1 我在学习Runnable接口多线程--2 我在吃饭--2 我在学习Runnable接口多线程--3 我在吃饭--3 我在学习Runnable接口多线程--4 我在吃饭--4 ……小结
继承Thread类
子类继承Thread类具备多线程能力启动线程:子类对象.start()不建议使用:避免OOP单继承局限性 实现Runnable接口
实现接口Runnable具有多线程能力启动线程:传入目标对象+Thread对象.start()推荐使用:避免单继承局限性,灵活方便,方便同一个对象被多个线程使用。 Callable
实现Callable接口,需要返回值类型重写call方法,需要抛出异常创建目标对象创建执行服务:ExecutorService ser = Executors.newFixedThreadPool(1);提交执行:Future result1 = ser.submit(t1);获取结果:boolean r1 = result1.get();关闭服务:ser.shutdownNow();
package multithreading; import java.util.concurrent.*; public class TestCallable implements Callable{ private String doWhat; public TestCallable(String doWhat){ this.doWhat = doWhat; } @Override public Boolean call() throws Exception { for (int i = 0; i < 10; i++) { System.out.println("我在"+doWhat+"-->"+i); } return true; } public static void main(String[] args) throws ExecutionException, InterruptedException { TestCallable t1 = new TestCallable("学习多线程"); TestCallable t2 = new TestCallable("吃饭"); TestCallable t3 = new TestCallable("唱歌"); //创建执行服务 ExecutorService ser = Executors.newFixedThreadPool(3); //提交执行 Future result1 = ser.submit(t1); Future result2 = ser.submit(t2); Future result3 = ser.submit(t3); //获取结果 boolean r1 = result1.get(); boolean r2 = result2.get(); boolean r3 = result3.get(); //关闭服务 ser.shutdownNow(); } }



