Java 多线程学习
}
}
Runnable:实现 Runnable 接口
定义 MyRunnable 类实现 Runnable 接口
实现 run()方法,编写线程执行体
创建线程对象,调用 start()方法启动线程
不建议使用 Thread,避免 OOP 单继承局限性,推荐使用 Runnable,方便同一个对象被多个线程使用。
package com.thread.lesson01;
//实现 runnable 接口,重写 run 方法,执行线程需要丢入 runable 接口实现类,调用 start 方法
public class TestThread03 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("我在读书!"+i);
}
}
public static void main(String[] args) {
//创建 runable 接口实现类对象
TestThread03 t3 = new TestThread03();
//创建线程对象 i
//Thread thread = new Thread(t3);
//thread.start();
new Thread(t3).start();
for (int i = 0; i < 100; i++) {
System.out.println("我在学多线程!"+i);
}
}
}
下载图片
package com.thread.lesson01;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
//练习 Runnable 实现多线程下载图片
public class TestThread04 implements Runnable{
private String url;
private String name;
public TestThread04(String url,String name){
this.url = url;//图片地址
this.name = name;//文件名
}
//下载图片线程执行体
@Override
public void run() {
WebDownLoaders webDownLoader = new WebDownLoaders();
webDownLoader.downloader(url,name);
System.out.println("下载了文件名为:"+name);
}
public static void main(String[] args) {
TestThread04 t1 = new TestThread04("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png","1.jpg");
TestThread04 t2 = new TestThread04("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png","2.jpg");
TestThread04 t3 = new TestThread04("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png","3.jpg");
new Thread(t1).start();
new Thread(t2).start();
new Thread(t3).start();
}
}
//下载器
class WebDownLoaders{
//下载方法
public void downloader(String url,String name) {
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IO 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 Exception e) {
e.printStackTrace();
}
}
}
模拟抢票
package com.thread.lesson01;
//多个线程同时操作同一个对象(买火车票)
//发现问题,多个进程操作同一个资源的情况下,线程不安全,数据紊乱
public class TestThread05 implements Runnable{
//票数
private int tickeNums = 10;
@Override
public void run() {
while (true){
if (tickeNums<=0){
break;
}
//模拟延时
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"拿到了第"+tickeNums--+"张票");
}
}
public static void main(String[] args) {
TestThread05 t5 = new TestThread05();
new Thread(t5,"小周").start();
new Thread(t5,"小秦").start();
new Thread(t5,"小王").start();
}
}
模拟龟兔赛跑
package com.thread.lesson01;
//模拟龟兔赛跑
public class Race implements Runnable{
//胜利者
private static String winner;
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
//模拟兔子休息
if (Thread.currentThread().getName().equals("兔子")&&i%20==0){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//判断比赛是否结束
boolean flag = gameOver(i);
//如果比赛结束 停止程序
if (flag){
break;
}
System.out.println(Thread.currentThread().getName()+"跑了"+i+"步");
}
}
//判断是否完成比赛
private boolean gameOver(int steps){
//判断是否有胜利者
if (winner!=null){//已经存在胜利者了
return true;
}{
if (steps>=100){
winner = Thread.currentThread().getName();
System.out.println("winner is"+winner);
return false;
}
}
return false;
}
public static void main(String[] args) {
Race race = new Race();
new Thread(race,"兔子").start();
new Thread(race,"乌龟").start();
}
}
Callable:实现 Callable 接口
了解即可
实现 Callable 接口,需要返回值类型
重写 call 方法,需要抛出异常
创建目标对象
创建执行服务:ExecutorService ser = Executors.newFixedThreadPool()
提交执行:Future?result1 = ser.submit()
获取结果:boolean r1 = result1.get()
关闭服务:ser.shutdownNow()
package com.thread.lesson01;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;
//实现 Callable 接口
//有返回值 抛出了异常
public class TestThread06 implements Callable<Boolean> {
private String url;
private String name;
public TestThread06(String url,String name){
this.url = url;//图片地址
this.name = name;//文件名
}
//下载图片线程执行体
@Override
public Boolean call() {
WebDownLoader3 webDownLoader = new WebDownLoader3();
webDownLoader.downloader(url,name);
System.out.println("下载了文件名为:"+name);
return true;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
评论