1.继承Thread类重写run方法
/**
* @author 守鹤
* @date 2020/5/24 16:28
*/
public class ByExtendThread extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+"正在执行--->"+i);
}
}
public static void main(String[] args) {
ByExtendThread thread1 = new ByExtendThread();
ByExtendThread thread2 = new ByExtendThread();
thread1.setName("子线程1");
thread2.setName("子线程2");
thread1.start();
thread2.start();
}
}
1.1.网图下载小案例
- 导入Common-io 依赖
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
/**
* @author 守鹤
* @date 2020/5/24 16:01
*/
public class ThreadDownLoad extends Thread {
//网络资源地址
private String url;
//下载之后的名字
private String name;
public ThreadDownLoad(String url, String name) {
this.url = url;
this.name = name;
}
@Override
public void run() {
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
System.out.println(name+"图片下载成功");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ThreadDownLoad load1 = new ThreadDownLoad("https://i0.hdslb.com/bfs/space/265ecddc52d74e624dc38cf0cff13317085aedf7.png@2200w_400h_1o.webp","1.jpg");
ThreadDownLoad load2= new ThreadDownLoad("https://i0.hdslb.com/bfs/space/265ecddc52d74e624dc38cf0cff13317085aedf7.png@2200w_400h_1o.webp","2.jpg");
ThreadDownLoad load3 = new ThreadDownLoad("https://i0.hdslb.com/bfs/space/265ecddc52d74e624dc38cf0cff13317085aedf7.png@2200w_400h_1o.webp","3.jpg");
load1.start();
load2.start();
load3.start();
}
}
图片下载成功,但是可以看到不是顺序下载的
2.实现Runable接口,重写run方法
/**
* @author 守鹤
* @date 2020/5/24 16:49
*/
public class ByImplRunable implements Runnable {
private Integer tickets=10;
public void run() {
while (true){
if(tickets>0){
System.out.println(Thread.currentThread().getName()+"正在买第"+tickets--+"张票");
}else{
break;
}
}
}
public static void main(String[] args) {
ByImplRunable run1 = new ByImplRunable();
new Thread(run1,"守鹤").start();
new Thread(run1,"死歌").start();
new Thread(run1,"青鸟").start();
}
}
2.1.龟兔赛跑小案例
/**
* @author 守鹤
* @date 2020/5/24 17:04
*/
public class RabbitRace implements Runnable {
private String winner;
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName()+"跑了"+i+"步");
if(gameOver(i)){
break;
}
}
}
public boolean gameOver(Integer step){
if(winner!=null){
return true;
}
if(step==100){
winner=Thread.currentThread().getName();
System.out.println(winner+"是获胜者");
return true;
}
return false;
}
public static void main(String[] args) {
RabbitRace race = new RabbitRace();
new Thread(race,"兔子").start();
new Thread(race,"乌龟").start();
}
}
3.实现Callable接口,重写call方法
package com.shouhe.demo;
import java.util.concurrent.*;
/**
* @author 守鹤
* @date 2020/5/24 17:29
*/
public class ByImplCallable implements Callable<Boolean> {
public Boolean call() throws Exception {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()+"--->"+i);
}
return true;
}
public static void main(String[] args) {
ByImplCallable c1= new ByImplCallable();
ByImplCallable c2 = new ByImplCallable();
ByImplCallable c3 = new ByImplCallable();
ExecutorService service = Executors.newFixedThreadPool(3);
Future<Boolean> sub1 = service.submit(c1);
Future<Boolean> sub2 = service.submit(c2);
Future<Boolean> sub3 = service.submit(c3);
try {
Boolean b1 = sub1.get();
Boolean b2 = sub2.get();
Boolean b3 = sub3.get();
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
service.shutdown();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}