关于java的Runnable接口
在前面的文章中,我们了解到了继承Thread类,重写run方法,用来实现多线程等知识,我们本篇文章来介绍一下创建线程方式的第二种,实现runnable接口😀。
一、Runnable
创建线程的第二种方式,通过实现Runnable接口,重写run方法,执行线程需要丢入runnable接口实现类,调用start方法。
1、首先我们创建一个新的类,TestThread3。
package thread;
public class TestThread3
{
}
2、类实现接口Runnable,重写run方法。
package thread;
public class TestThread3 implements Runnable{
@Override
public void run() {
}
}
3、对比Thread的多线程实现。
- Thread
package thread;
public class TestThread1 extends Thread{
@Override
public void run() {
//run方法线程体
for (int i = 0; i < 10; i++) {
System.out.println("学习线程--"+i);
}
}
public static void main(String[] args) {
//创建一个线程对象
TestThread1 testThread1 = new TestThread1();
//调用start方法,开启线程
testThread1.run();
//主线程
for (int i = 0; i < 10; i++) {
System.out.println("看代码--"+i);
}
}
}
- Runnable
- 这种实现方法称之为代理。
package thread;
public class TestThread3 implements Runnable{
@Override
public void run() {
//run方法线程体
for (int i = 0; i < 10; i++) {
System.out.println("学习线程--"+i);
}
}
public static void main(String[] args) {
//创建一个线程对象
TestThread3 testThread3 = new TestThread3();
//创建Thread类的对象
Thread thread = new Thread(testThread3);//在构造函数中传参
//调用start方法,开启线程
thread.start();
//new Thread(testThread3).start();//可以简写
//主线程
for (int i = 0; i < 10; i++) {
System.out.println("看代码--"+i);
}
}
}
4、执行以下代码。
看代码--0
看代码--1
看代码--2
看代码--3
看代码--4
学习线程--0
看代码--5
看代码--6
学习线程--1
学习线程--2
学习线程--3
学习线程--4
学习线程--5
学习线程--6
学习线程--7
学习线程--8
学习线程--9
看代码--7
学习线程--10
学习线程--11
学习线程--12
学习线程--13
学习线程--14
学习线程--15
学习线程--16
学习线程--17
学习线程--18
学习线程--19
看代码--8
看代码--9
看代码--10
看代码--11
看代码--12
看代码--13
看代码--14
看代码--15
看代码--16
看代码--17
看代码--18
看代码--19
5、Thread底层实现方法。
public Thread(Runnable target) //构造函数
{
init(null, target, "Thread-" + nextThreadNum(), 0);
}
6、Runnable底层实现。
public interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
二、网图下载用Runnable实现
- 用Thread类实现的网图下载代码。
package thread;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class TestThread2 extends Thread {
private String url;
private String name;
public TestThread2(String url, String name) {
this.url = url;
this.name = name;
}
@Override
public void run() {
WebDownloader webDownloader = new WebDownloader();
webDownloader.downloader(url,name);
System.out.println("下载了文件名为:"+name);
}
public static void main(String[] args) {
TestThread2 t1 = new TestThread2("https://img.alicdn.com/imgextra/i3/95764894/O1CN01QztdCZ1m1VDPQnle3_!!0-saturn_solar.jpg_468x468q75.jpg_.webp","1.jpg");
TestThread2 t2 = new TestThread2("https://img.alicdn.com/imgextra/i3/95764894/O1CN01QztdCZ1m1VDPQnle3_!!0-saturn_solar.jpg_468x468q75.jpg_.webp","2.jpg");
t1.start();
t2.start();
}
}
//下载器
class WebDownloader{
//下载方法
public void downloader(String url,String name)
{
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO异常");
}
}
}
- 用Runnable接口实现代码。
package thread;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class TestThread2 implements Runnable //类实现接口
{
private String url;
private String name;
public TestThread2(String url, String name) {
this.url = url;
this.name = name;
}
@Override
public void run() {
WebDownloader webDownloader = new WebDownloader();
webDownloader.downloader(url,name);
System.out.println("下载了文件名为:"+name);
}
public static void main(String[] args) {
TestThread2 t1 = new TestThread2("https://img.alicdn.com/imgextra/i3/95764894/O1CN01QztdCZ1m1VDPQnle3_!!0-saturn_solar.jpg_468x468q75.jpg_.webp","1.jpg");
TestThread2 t2 = new TestThread2("https://img.alicdn.com/imgextra/i3/95764894/O1CN01QztdCZ1m1VDPQnle3_!!0-saturn_solar.jpg_468x468q75.jpg_.webp","2.jpg");
new Thread(t1).start();//区别就在这里
new Thread(t2).start();
}
}
//下载器
class WebDownloader{
//下载方法
public void downloader(String url,String name)
{
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO异常");
}
}
}
三、总结
- 继承Thread类
1、子类继承Thread类具备多线程能力,重写run方法。
2、启用线程:子类对象.start()
3、不建议使用:避免OOP单继承局限性。
- 实现Runnable接口
1、实现Runnable具备多线程能力。
2、启用线程:传入的目标对象+Thread对象.start()
3、推荐使用:避免单继承局限性,灵活方便,方便同一个对象被多个线程使用。