前言
创建线程有三种方式:
1.继承Thread
- 实现Runnable
- 实现Callable
具体代码
1.继承Thread:继承这种方式不推荐用,因为继承Thread之后便不能继承其他类,导致很局限
public void run(){//线程入口点
for(int i=0;i<20;i++)
System.out.println("啦啦啦");
}
public static void main(String[] args) {
Thread01 st=new Thread01();
st.start();//不一定立刻运行。由cpu决定
for(int i=0;i<20;i++)
System.out.println("插队插队");
}
}
2.实现Runnable:推荐使用方式
public void run(){//线程入口点
for(int i=0;i<20;i++)
System.out.println("啦啦啦");
}
public static void main(String[] args) {
Thread01 st=new Thread01();
Thread t=new Thread(st);//代理类对象
t.start();//不一定立刻运行。由cpu决定
for(int i=0;i<20;i++)
System.out.println("插队插队");
}
}
用第二种方式模仿抢票和龟兔赛跑:
抢票:(此处并发问题将在后期的文章讲解)
package thread;
public class Ticket_garbbing implements Runnable{
private int tickrtSize=100;
@Override
public void run() {
while(true) {
if (tickrtSize < 0) {
break;
}
try {
Thread.sleep(200);//模仿网络延时此时出现负数出现并发问题
} catch (InterruptedException e) {//并发需要保证线程安全
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"/"+tickrtSize--);
}
}
public static void main(String[] args) {
Ticket_garbbing tg1=new Ticket_garbbing();
new Thread(tg1,"io1").start();
new Thread(tg1,"io2").start();
new Thread(tg1,"io3").start();
new Thread(tg1,"io4").start();
new Thread(tg1,"io5").start();
new Thread(tg1,"io6").start();
}
}
龟兔赛跑:
package thread;
/*
* 龟兔赛跑
* */
public class Racter implements Runnable{
//胜利者
private static String winner;
@Override
public void run() {
//假设赛道长100米兔子每过10s睡一次觉
for(int i=0;i<=100;i++){
if(i%10==0&&Thread.currentThread().getName().equals("rabbit")){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"-->"+i);
boolean flag=gameOver(i);
if(flag){
System.out.println("winner is "+winner);
break;
}
}
}
//判断是否到达终点
private boolean gameOver(int i){
if(winner!=null){
return true;
}else{
if(i==100){
winner=Thread.currentThread().getName();
return true;
}else {
return false;
}
}
}
public static void main(String[] args) {
Racter racter=new Racter();
new Thread(racter,"rabbit").start();
new Thread(racter,"tortoise").start();
}
}
实现Callable(一般实际开发中使用的方式)
import java.util.concurrent.*;
public class NewClassable implements Callable<Boolean> {
private String path;
public NewClassable(String path){
this.path=path;
}
@Override
public Boolean call() throws Exception {
System.out.println(path);
down();
return true;
}
private void down(){
System.out.println("图片下载");
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
NewClassable n1=new NewClassable("a");
NewClassable n2=new NewClassable("b");
NewClassable n3=new NewClassable("c");
//创建执行任务
ExecutorService service= Executors.newFixedThreadPool(3);
//提交服务
Future<Boolean> rt1=service.submit(n1);
Future<Boolean> rt2=service.submit(n2);
Future<Boolean> rt3=service.submit(n3);
//获取结果
boolean r1=rt1.get();
boolean r2=rt2.get();
boolean r3=rt3.get();
//关闭服务
service.shutdownNow();
}
}
此文章来至个人视频资料整理,请多指教.
作者:欲指_Object
来源:CSDN
原文:https://blog.csdn.net/apple596529/article/details/89449905
版权声明:本文为博主原创文章,转载请附上博文链接!
网友评论