三种
- Thread class :继承Thread类
- Runnable 接口:实现Runnable接口、
- Callable接口:实现Callable接口
Thread
- 自定义线程类继承Thread
- 重写run()方法,编写线程执行体
- 创建线程对象,调用start()方法启动线程
package com.kai.lesson1;
public class TestThread1 extends Thread{
@Override
public void run() {
for (int i=0;i<20;i++){
System.out.println("rum方法");
}
}
public static void main(String[] args) {
//main线程,主线程
TestThread1 testThread1=new TestThread1();
testThread1.start();
for (int i=0;i<20000;i++){
System.out.println("主线程");
}
}
}
- 线程开启不一定立即执行,由CPU安排调度
例子
package com.kai.lesson1;
//联系Thread,实现多线程同步下载图片
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
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();
try {
webDownloader.dowanloader(url,name);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("下载了文件名为:"+name);
}
public static void main(String[] args) {
TestThread2 t1=new TestThread2("http://i0.hdslb.com/bfs/bangumi/image/86fdce81522f76b8b323776c7c77f044e13fc808.png","2.jpg");
TestThread2 t2=new TestThread2("http://i0.hdslb.com/bfs/bangumi/image/86fdce81522f76b8b323776c7c77f044e13fc808.png","3.jpg");
TestThread2 t3=new TestThread2("http://i0.hdslb.com/bfs/bangumi/image/86fdce81522f76b8b323776c7c77f044e13fc808.png","4.jpg");
t1.start();
t2.start();
t3.start();
}
}
//下载器
class WebDownloader{
//下载方法
public void dowanloader(String url, String name) throws IOException {
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IOy异常,downloader方法出现问题");
}
}
}
Runnable
创建线程方式:
- 实现runnable接口
- 重写run方法
- 执行线程需要丢入runnable接口实现类,调用start方法
package com.kai.lesson1;
public class TestThread3 implements Runnable {
@Override
public void run() {
for (int i=0;i<20;i++){
System.out.println("rum方法");
}
}
public static void main(String[] args) {
//创建runnable接口实现类对象
TestThread3 testThread3=new TestThread3();
//创建线程对象,通过线程对象开启线程,代理
//Thread thread=new Thread(testThread3);
//thread.start();
new Thread(testThread3).start();
for (int i=0;i<20000;i++){
System.out.println("主线程");
}
}
}
例子
package com.kai.lesson1;
//联系Thread,实现多线程同步下载图片
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
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();
try {
webDownloader.dowanloader(url,name);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("下载了文件名为:"+name);
}
public static void main(String[] args) {
TestThread2 t1=new TestThread2("http://i0.hdslb.com/bfs/bangumi/image/86fdce81522f76b8b323776c7c77f044e13fc808.png","22.jpg");
TestThread2 t2=new TestThread2("http://i0.hdslb.com/bfs/bangumi/image/86fdce81522f76b8b323776c7c77f044e13fc808.png","33.jpg");
TestThread2 t3=new TestThread2("http://i0.hdslb.com/bfs/bangumi/image/86fdce81522f76b8b323776c7c77f044e13fc808.png","44.jpg");
/*
t1.start();
t2.start();
t3.start();
*/
new Thread(t1).start();
new Thread(t2).start();
new Thread(t3).start();
}
}
//下载器
class WebDownloader{
//下载方法
public void dowanloader(String url, String name) throws IOException {
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IOy异常,downloader方法出现问题");
}
}
}