线程
线程实现
三种创建方式
//继承Thread 类
//实现Runnable接口
//实现Callable接口
继承Thread类
//方式一:继承Thread类,重写run()方法,调用start()方法开启线程
//不建议使用,避免OOP单继承局限性
//总结:注意,线程开启不一定立即执行,由cpu调度执行
public class myThread1 extends Thread{
@Override
public void run() {
//run方法线程体
for (int i = 0; i < 20; i++) {
System.out.println("我在看代码----" + i);
}
}
public static void main(String[] args) {
//main线程,主线程
//创建一个线程对象
myThread1 mythread = new myThread1();
//调用start方法开启线程
mythread.start();
for (int i = 0; i < 20; i++) {
System.out.println("我在学习多线程----" + i);
}
}
}
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
//练习Thread,实现多线程同步下载图片
public class MyThread2 extends Thread{
private String url; //网络图片地址
private String name; //保存的文件名
public MyThread2(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) {
MyThread2 m1 = new MyThread2("https://inews.gtimg.com/newsapp_bt/0/13688635315/1000","1.jpg");
MyThread2 m2 = new MyThread2("https://inews.gtimg.com/newsapp_bt/0/13688635315/1000","2.jpg");
MyThread2 m3 = new MyThread2("https://inews.gtimg.com/newsapp_bt/0/13688635315/1000","3.jpg");
m1.start();
m2.start();
m3.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("downloader方法出现异常");
}
}
}
实现Runnable接口
//推荐使用,避免单继承局限性,灵活方便,方便同一个对象被多个线程使用
//创建线程方式二:实现runnabe接口,重写run方法,执行线程需要丢入runnable接口实现类,调用start方法
public class MyThread3 implements Runnable{
@Override
public void run() {
//run方法线程体
for (int i = 0; i < 20; i++) {
System.out.println("我在看代码----" + i);
}
}
public static void main(String[] args) {
//创建runnable接口的实现类对象
MyThread3 myThread3 = new MyThread3();
//创建线程对象,通过线程对象来开启我们的线程,代理
// Thread thread = new Thread(myThread3);
// thread.start();
new Thread(myThread3).start();
for (int i = 0; i < 20; i++) {
System.out.println("我在学习多线程----" + i);
}
}
}
//模拟龟兔赛跑
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("rabbit") && i == 40) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(gameOver(i))
break;
System.out.println(Thread.currentThread().getName()+"-->跑了"+i+"步");
}
}
private boolean gameOver(int steps){
//判断是否有胜利者
if(winner!=null){
return true;
}else if(steps >= 100){
winner = Thread.currentThread().getName();
System.out.println("winner is " + winner);
return true;
}
return false;
}
public static void main(String[] args) {
Race race = new Race();
new Thread(race,"turtle").start();
new Thread(race,"rabbit").start();
}
}
实现Callable接口
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;
//线程创建方式三:实现Callable接口
/*
callable的好处
1.可以定义返回值
2.可以抛出异常
*/
public class MyCallable implements Callable<Boolean> {
private String url; //网络图片地址
private String name; //保存的文件名
public MyCallable(String url,String name){
this.url = url;
this.name = name;
}
//下载图片的执行体
@Override
public Boolean call() {
WebDownloader2 webDownloader2 = new WebDownloader2();
webDownloader2.downloader(url,name);
System.out.println("下载了文件:" + name);
return true;
}
public static void main(String[] args) {
MyCallable m1 = new MyCallable("https://inews.gtimg.com/newsapp_bt/0/13688635315/1000","1.jpg");
MyCallable m2 = new MyCallable("https://inews.gtimg.com/newsapp_bt/0/13688635315/1000","2.jpg");
MyCallable m3 = new MyCallable("https://inews.gtimg.com/newsapp_bt/0/13688635315/1000","3.jpg");
//创建执行服务
ExecutorService ser = Executors.newFixedThreadPool(3);
//提交执行
Future<Boolean> r1 = ser.submit(m1);
Future<Boolean> r2 = ser.submit(m2);
Future<Boolean> r3 = ser.submit(m3);
//获取结果
try {
boolean rs1 = r1.get();
boolean rs2 = r2.get();
boolean rs3 = r3.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
//关闭服务
ser.shutdownNow();
}
}
class WebDownloader2{
//下载方法
public void downloader(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("downloader方法出现异常");
}
}
}
静态代理模式
//静态代理模式
//真实对象和代理对象实现同一个接口
//代理对象要代理真实角色
//好处:
// 代理对象可以做很多真实对象做不了的事情
// 真实对象专注做自己的事情
public class StaticProxy {
public static void main(String[] args) {
//实现runnable接口的底层就是静态代理模式
new Thread(()-> System.out.println("我爱你")).start();
//静态代理
new WeddingCompany(new You()).HappyMarry();
}
}
interface Marry{
void HappyMarry();
}
//真实角色
class You implements Marry{
@Override
public void HappyMarry() {
System.out.println("结婚咯");
}
}
//代理角色
class WeddingCompany implements Marry{
private Marry target;
public WeddingCompany(Marry target){
this.target = target;
}
@Override
public void HappyMarry() {
before();
target.HappyMarry();
after();
}
private void after() {
System.out.println("结婚后");
}
private void before() {
System.out.println("结婚前");
}
}
lamda表达式
/*
推导lambda表达式
*/
/*
lambda表达式
必须在函数式接口中
函数式接口是只有一个方法的接口,Runnable接口就是函数式接口
可以避免过多的匿名内部类
*/
public class TestLambda {
//2、静态内部类
static class Like2 implements ILike{
@Override
public void lambda() {
System.out.println("i like lambda2");
}
}
public static void main(String[] args) {
//1、接口实现
ILike like = new Like();
like.lambda();
//2、静态内部类
like = new Like2();
like.lambda();
//3、局部内部类
class Like3 implements ILike{
@Override
public void lambda() {
System.out.println("i like lambda3");
}
}
like = new Like3();
like.lambda();
//4、匿名内部类,没有类的名称,必须借助接口或者父类
like = new ILike() {
@Override
public void lambda() {
System.out.println("i like lambda4");
}
};
like.lambda();
//5、用lambda简化
like = ()->{
System.out.println("i like lambda5");
};
like.lambda();
}
}
interface ILike{
void lambda();
}
//1、接口实现
class Like implements ILike{
@Override
public void lambda() {
System.out.println("i like lambda");
}
}
public class TestLambda2 {
public static void main(String[] args) {
ILove love = null;
love = (int a)->{
System.out.println("love " + a);
};
love.loveWho(1);
//简化掉参数类型,可以去掉多个,但是要去掉就要都去掉
love = (a)->{
System.out.println("love " + a);
};
love.loveWho(2);
//只有一个参数可以简化掉小括号
love = a->{
System.out.println("love " + a);
};
love.loveWho(3);
//只有一行代码可以简化掉花括号
love = a->System.out.println("love " + a);
love.loveWho(4);
}
}
interface ILove{
void loveWho(int a);
}
线程状态
停止线程
//测试Stop
//1、建议线程正常停止--->利用次数,不建议死循环。
//2、建议使用标志位--->设置一个标志位
//3、不要使用Stop或者destroy等过时或者JDK不建议使用的方法
public class ThreadStop implements Runnable{
//1、设置一个标志位
private boolean flag = true;
@Override
public void run() {
int i = 0;
while (flag){
System.out.println("run...thread" + i++);
}
}
//2、设置一个公开的方法停止线程,转换标志位
public void stop(){
this.flag = false;
}
public static void main(String[] args) {
ThreadStop threadStop = new ThreadStop();
new Thread(threadStop).start();
for (int i = 0; i < 1000; i++) {
System.out.println("main" + i);
if(i == 900){
//转换标志位
threadStop.stop();
System.out.println("线程停止了");
}
}
}
}
线程休眠
//主要的俩个作用,模拟倒计时和网络延时
//每一个对象都有一个锁,sleep不会释放锁
//模拟网络延时 : 放大问题的发生性
public class ThreadSleep implements Runnable{
//票数
private int ticketNums = 10;
@Override
public void run() {
while (true){
if(ticketNums <= 0)
break;
//模拟延时
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"--->拿到了第"+ticketNums--+"张票");
}
}
public static void main(String[] args) {
ThreadSleep ticket = new ThreadSleep();
new Thread(ticket,"蛮蛮").start();
new Thread(ticket,"湛").start();
new Thread(ticket,"海绵宝宝").start();
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
//模拟倒计时
public class ThreadSleep2 {
public static void tenDown() throws InterruptedException {
int num = 10;
while (true) {
Thread.sleep(1000);
System.out.println(num--);
if (num <= 0)
break;
}
}
public static void main(String[] args) {
try {
tenDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
//打印当前系统时间
Date startTime = new Date(System.currentTimeMillis());
while (true){
try {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
startTime = new Date(System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
线程礼让
//测试礼让线程
//礼让不一定成功,看cpu心情
//礼让会让当前正在执行的线程暂停,但不阻塞,从运行状态转为就绪状态
public class ThreadYield {
public static void main(String[] args) {
MyYield myYield = new MyYield();
new Thread(myYield,"a").start();
new Thread(myYield,"b").start();
}
}
class MyYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "开始执行");
Thread.yield();
System.out.println(Thread.currentThread().getName() + "结束执行");
}
}
Join合并线程
//测试Join方法 可以想象成插队 强制执行,让其他线程阻塞,跑完其他才能跑
public class ThreadJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("线程vip来了 " + i);
}
}
public static void main(String[] args) throws InterruptedException {
ThreadJoin threadJoin = new ThreadJoin();
Thread thread = new Thread(threadJoin);
thread.start();
//主线程
for (int i = 0; i < 500; i++) {
if(i == 200){
thread.join();
}
System.out.println("main " + i);
}
}
}
线程状态观测
//观察测试线程的状态
public class ThreadState {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("//");
}
});
//观察状态
Thread.State state = thread.getState();
System.out.println(state); //NEW
thread.start();
state = thread.getState();
System.out.println(state); //RUN
while(state != Thread.State.TERMINATED){ //只要线程不终止,一直输出状态
Thread.sleep(100);
state = thread.getState();
System.out.println(state);
}
}
}
线程优先级
//测试线程优先级
/*
范围1~10
Thread.MAX_PRIORITY = 10;
Thread.MIN_PRIORITY = 1;
Thread.NORM_PRIORITY = 5;
getPriority();setPriority(int xx);
*/
//优先级低只意味着获得调度的概率低,不是不被调用,要看cpu
public class ThreadPriorityz {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + "-->" + Thread.currentThread().getPriority());
MyPriority myPriority = new MyPriority();
Thread t1 = new Thread(myPriority);
Thread t2 = new Thread(myPriority);
Thread t3 = new Thread(myPriority);
Thread t4 = new Thread(myPriority);
Thread t5 = new Thread(myPriority);
//先设置优先级,再启动
t1.start();
t2.setPriority(1);
t2.start();
t3.setPriority(4);
t3.start();
t4.setPriority(Thread.MAX_PRIORITY); //10
t4.start();
t5.setPriority(6);
t5.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "-->" + Thread.currentThread().getPriority());
}
}
守护(daemon)线程
//测试守护线程
//上帝守护你
public class ThreadDaemon {
public static void main(String[] args) {
God god = new God();
You you = new You();
Thread thread = new Thread(god);
thread.setDaemon(true); //默认false是用户线程,一般都是用户线程
thread.start();
new Thread(you).start();
}
}
class God implements Runnable{
@Override
public void run() {
System.out.println("God bless you");
}
}
class You implements Runnable{
@Override
public void run() {
for (int i = 0; i < 36500; i++) {
System.out.println("开心的活着");
}
System.out.println("======goodbye,world======");
}
}
线程同步
并发
锁机制synchronized
三大不安全例子
//不安全的买票
public class UnSafeBuyTicket {
public static void main(String[] args) {
BuyTicket station = new BuyTicket();
new Thread(station,"蛮蛮").start();
new Thread(station,"湛").start();
new Thread(station,"海绵宝宝").start();
}
}
class BuyTicket implements Runnable{
private int ticketNums = 10;
boolean flag = true; //外部停止方式
@Override
public void run() {
//买票
while (flag){
try {
Thread.sleep(100);
buy();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//synchonized 同步方法,锁的是this
private synchronized void buy() throws InterruptedException {
//判断是否有票
if(ticketNums <= 0){
flag = false;
return;
}
// Thread.sleep(100);
System.out.println(Thread.currentThread().getName() + "拿到" + ticketNums--);
}
}
//不安全的取钱
//俩个去银行取钱,账户
public class UnSafeBank {
public static void main(String[] args) {
//账户
Account account = new Account(100,"结婚基金");
Drawing you = new Drawing(account,50,"湛");
Drawing man = new Drawing(account,100,"蛮");
you.start();
man.start();
}
}
class Account{
int money; // 余额
String name; // 卡名
public Account(int money, String name) {
this.money = money;
this.name = name;
}
}
class Drawing extends Thread{
//账户
Account account;
//取了多少钱
int drawingMoney;
public Drawing(Account account,int drawingMoney,String name){
super(name);
this.account = account;
this.drawingMoney = drawingMoney;
}
//取钱
//synchronized 默认锁this
@Override
public void run() {
//同步块
synchronized (account){
//判断有没有钱
//锁的对象是要变化的对象,需要增删改的对象
if(account.money - drawingMoney < 0){
System.out.println(Thread.currentThread().getName() + " 钱不够,取不了");
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//卡内余额 = 余额 - 取了的钱
account.money = account.money - drawingMoney;
System.out.println(account.name + " 余额为: " + account.money);
}
}
}
import java.util.ArrayList;
import java.util.List;
//线程不安全的集合
public class UnSafeList {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
for (int i = 0; i < 100000; i++) {
new Thread(()->{
synchronized (list){
list.add(Thread.currentThread().getName());
}
}).start();
}
try { //加延时,等集合的线程跑完
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(list.size());
}
}
synchronized锁
//同步方法
public synchronized void xxx(){}
//同步块
synchronized(xx){}
import java.util.concurrent.CopyOnWriteArrayList;
//测试JUC安全类型的集合,并发包
public class TestJUC {
public static void main(String[] args) throws InterruptedException {
CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
for (int i = 0; i < 10000; i++) {
new Thread(()->{
list.add(Thread.currentThread().getName());
}).start();
}
Thread.sleep(1000);
System.out.println(list.size());
}
}
死锁
//死锁:多个线程互相占用资源,互相等待
//死锁的四个必要条件
/*
1、互斥:一个资源一次只能被一个进程使用
2、请求与保持:一个进程因请求资源而阻塞时,对已获得的资源保持不放
3、不剥夺:进程已获得的资源,在未使用完之前,不能强行剥夺
4、循环等待:若干进程之间形成一种头尾相接的循环等待资源关系
*/
public class DeadLock {
public static void main(String[] args) {
MakeUp g1 = new MakeUp(0,"bitch");
MakeUp g2 = new MakeUp(1,"mizuki");
g1.start();
g2.start();
}
}
//口红
class Lipstick{
}
//镜子
class Mirror{
}
class MakeUp extends Thread{
//需要的资源只有一份,用static来保证只有一份
static Lipstick lipstick = new Lipstick();
static Mirror mirror = new Mirror();
int choice;
String name;
public MakeUp(int choice,String name){
this.choice = choice;
this.name = name;
}
@Override
public void run() {
//化妆
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// //化妆,互相持有对方的锁,就是需要拿到对方的资源
// private void makeup() throws InterruptedException {
// if(choice == 0){
// synchronized (lipstick){
// System.out.println(this.name + "获得了口红");
// Thread.sleep(1000);
// synchronized (mirror){
// System.out.println(this.name + "获得了镜子");
// }
// }
// }else{
// synchronized (mirror){
// System.out.println(this.name + "获得了镜子");
// Thread.sleep(2000);
// synchronized (lipstick){
// System.out.println(this.name + "获得了口红");
// }
// }
// }
// }
//化妆,不在占有的时候等待,可以正常
private void makeup() throws InterruptedException {
if(choice == 0){
synchronized (lipstick){
System.out.println(this.name + "获得了口红");
Thread.sleep(1000);
}
synchronized (mirror){
System.out.println(this.name + "获得了镜子");
}
}else{
synchronized (mirror){
System.out.println(this.name + "获得了镜子");
Thread.sleep(2000);
}
synchronized (lipstick){
System.out.println(this.name + "获得了口红");
}
}
}
}
Lock(锁)
import java.util.concurrent.locks.ReentrantLock;
//Lock锁,可重入锁
//Lock是显式锁,要手动打开和关闭,记得要手动关闭
//synchronized是隐式锁,出了作用域自动释放
//lock只有代码块锁,synchronized有代码块锁和方法锁
//使用lock锁,JVM花较少时间来调度线程,性能更好。并具有更好的可扩展性
//Lock > 同步代码块(已经进入了方法体,分配了相应资源) > 同步方法(在方法体之外)
public class TestLock {
public static void main(String[] args) {
TestLock2 testLock2 = new TestLock2();
new Thread(testLock2).start();
new Thread(testLock2).start();
new Thread(testLock2).start();
}
}
class TestLock2 implements Runnable{
int ticketNums = 10;
//定义Lock锁
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
while (true){
try {
lock.lock();
if(ticketNums > 0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(ticketNums--);
}else {
break;
}
}finally {
lock.unlock();
}
}
}
}
线程协作
生产者消费者模式
线程通信
//测试:生产者消费者模式----->利用缓冲区解决:管程法
//生产者,消费者,产品,缓冲区
public class TestPC {
public static void main(String[] args) {
SynContainer container = new SynContainer();
new Productor(container).start();
new Consumer(container).start();
}
}
//生产者
class Productor extends Thread{
SynContainer container;
public Productor(SynContainer container){
this.container = container;
}
//生产
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("生产了" + i + "只鸡");
container.push(new Chicken(i));
}
}
}
//消费者
class Consumer extends Thread{
SynContainer container = new SynContainer();
public Consumer(SynContainer container) {
this.container = container;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("消费了第" + container.pop().id + "只鸡");
}
}
}
//产品
class Chicken {
int id;
public Chicken(int id) {
this.id = id;
}
}
//缓冲区
class SynContainer{
//需要一个容器大小
Chicken[] chickens = new Chicken[10];
//容器计数器
int count = 0;
//生产者放入产品
public synchronized void push(Chicken chicken){
//如果容器满了,就需要等待消费
if(count == chickens.length){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果没有满,生产产品
chickens[count] = chicken;
count++;
//可以通知消费了
this.notifyAll();
}
//消费者消费产品
public synchronized Chicken pop(){
//判断能否消费
if(count == 0){
//等待生产者生产,消费者等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果可以消费
count--;
Chicken chicken = chickens[count];
//吃完了,通知生产
this.notifyAll();
return chicken;
}
}
//生产者消费者问题,信号灯法,标志位解决
public class TestPC2 {
public static void main(String[] args) {
TV tv = new TV();
new Player(tv).start();
new Watcher(tv).start();
}
}
//生产者-->演员
class Player extends Thread{
TV tv;
public Player(TV tv) {
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if(i%2 == 0){
this.tv.play("东京热");
}else {
this.tv.play("广告");
}
}
}
}
//消费者-->观众
class Watcher extends Thread{
TV tv;
public Watcher(TV tv) {
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
this.tv.watch();
}
}
}
//产品-->节目
class TV{
//演员表演,关注等待
//观众观看,演员等待
String voice;//表演的节目
boolean flag = true;
//表演
public synchronized void play(String voice){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("演员表演了" + voice);
//通知观众观看
this.notifyAll(); //通知唤醒
this.voice = voice;
this.flag = !this.flag;
}
//观看
public synchronized void watch(){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("观看了" + voice);
this.notifyAll();
this.flag = !this.flag;
}
}
线程池
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//测试线程池
public class ThreadPool {
public static void main(String[] args) {
//1、创建服务,创建线程池
//newFixedThreadPool 参数为线程池大小
ExecutorService service = Executors.newFixedThreadPool(10);
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
//关闭连接
service.shutdown();
}
}
class MyThread implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
创建总结
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
//回顾总结线程的创建
public class ThreadConclude {
public static void main(String[] args) {
new MyThread1().start();
new Thread(new MyThread2()).start();
FutureTask<Integer> futureTask = new FutureTask<Integer>(new MyThread3());
new Thread(futureTask).start();
try {
Integer res = futureTask.get();
System.out.println(res);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
//1、继承Thread类
class MyThread1 extends Thread{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" 继承Thread类");
}
}
//2、实现Runnabel接口
class MyThread2 implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" 实现Runnable接口");
}
}
//3、实现Callable接口
class MyThread3 implements Callable<Integer>{
@Override
public Integer call() throws Exception {
System.out.println(Thread.currentThread().getName()+" 实现Callable接口");
return 100;
}
}