多线程的实现方式及简单模拟

多线程的实现方式及简单模拟

多线程的实现方式

  1. 继承Thread类
    • 1、创建:继承Thread + 重写run
    • 2、启动:创建子类对象 + start
  2. Runnable接口(避免单继承的局限性,优先使用接口
    • 1、创建:实现Runnable + 重写run
    • 2、启动:创建实现类对象 + Thread对象 + start
  3. Callable接口
    • 1、创建目标对象
    • 2、创建执行服务
    • 3、提交执行
    • 4、获取结果
    • 5、关闭服务

多线程的简单模拟

模拟12306

代码如下:

package com.msl.thread;
/**
 * 模拟12306
 * @author Senley
 *
 */
public class Web12306 implements Runnable {
    //票数
    private int ticketNums = 99;
    
    @Override
    public void run() {
        while(true) {
            if(ticketNums<0) {
                break;
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"-->"+ticketNums--);
        }
    }
    
    public static void main(String[] args) {
        //一份资源
        Web12306 web = new Web12306();
        System.out.println(Thread.currentThread().getName());
        //多个代理
        new Thread(web,"码畜").start();
        new Thread(web,"码农").start();
        new Thread(web,"码蟥").start();
    }
}

结果如下:

main
码畜-->99
码蟥-->97
码农-->98
码农-->96
码蟥-->96
码畜-->96
码畜-->95
码农-->95
码蟥-->95
码畜-->94
码农-->94
码蟥-->93
码蟥-->92
码农-->91
码畜-->90
码畜-->89
码农-->88
码蟥-->88
码蟥-->87
码畜-->86
码农-->85
码蟥-->84
码农-->84
码畜-->83
码畜-->82
码蟥-->82
码农-->82
码蟥-->80
码农-->81
码畜-->81
码畜-->79
码蟥-->78
码农-->79
码农-->77
码畜-->77
码蟥-->77
码畜-->76
码农-->74
码蟥-->75
码农-->73
码蟥-->72
码畜-->73
码农-->71
码畜-->71
码蟥-->70
码农-->69
码蟥-->68
码畜-->69
码蟥-->66
码农-->67
码畜-->65
码蟥-->62
码农-->63
码畜-->64
码畜-->61
码蟥-->60
码农-->59
码畜-->58
码蟥-->57
码农-->58
码农-->56
码畜-->56
码蟥-->56
码蟥-->55
码农-->55
码畜-->54
码蟥-->53
码农-->51
码畜-->52
码畜-->50
码蟥-->50
码农-->50
码农-->48
码蟥-->49
码畜-->47
码畜-->44
码农-->45
码蟥-->46
码畜-->43
码农-->41
码蟥-->42
码农-->39
码畜-->40
码蟥-->40
码畜-->38
码农-->38
码蟥-->38
码畜-->37
码农-->37
码蟥-->37
码蟥-->35
码农-->35
码畜-->36
码畜-->34
码蟥-->34
码农-->34
码农-->33
码畜-->33
码蟥-->33
码蟥-->32
码农-->32
码畜-->32
码畜-->31
码农-->31
码蟥-->31
码蟥-->30
码畜-->30
码农-->30
码农-->29
码蟥-->28
码畜-->29
码畜-->27
码农-->26
码蟥-->27
码畜-->25
码农-->25
码蟥-->25
码蟥-->24
码农-->23
码畜-->23
码蟥-->22
码农-->22
码畜-->22
码畜-->21
码蟥-->19
码农-->20
码蟥-->18
码畜-->18
码农-->17
码蟥-->16
码农-->15
码畜-->14
码畜-->13
码农-->13
码蟥-->13
码蟥-->12
码农-->11
码畜-->12
码农-->10
码畜-->9
码蟥-->8
码农-->7
码畜-->6
码蟥-->7
码畜-->5
码农-->3
码蟥-->4
码蟥-->2
码农-->2
码畜-->2
码畜-->0
码农-->1
码蟥-->1

模拟龟兔赛跑

代码如下:

package com.msl.thread;
/**
 * 模拟龟兔赛跑
 * @author Senley
 *
 */
public class Racer implements Runnable{
    private String winner;//胜利者
    @Override
    public void run() {
        for(int steps=1;steps<=100;steps++) {
            //模拟休息
            if(Thread.currentThread().getName().equals("rabbit")&&steps%10==0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+"-->"+steps);
            //比赛是否结束
            boolean flag = gameOver(steps);
            if(flag) {
                break;
            }
        }
    }
    
    private boolean gameOver(int steps) {
        if(winner!=null) {//存在胜利者
            return true;
        }else {
            if(steps==100) {
                winner = Thread.currentThread().getName();
                System.out.println("winner-->"+winner);
                return true;
            }
        }
        return false;
    }
    
    public static void main(String[] args) {
        Racer racer = new Racer();
        new Thread(racer,"tortoise").start();
        new Thread(racer,"rabbit").start();
    }
}

结果如下:

tortoise-->1
rabbit-->1
rabbit-->2
rabbit-->3
rabbit-->4
rabbit-->5
rabbit-->6
rabbit-->7
rabbit-->8
rabbit-->9
tortoise-->2
tortoise-->3
tortoise-->4
tortoise-->5
tortoise-->6
tortoise-->7
tortoise-->8
tortoise-->9
tortoise-->10
tortoise-->11
tortoise-->12
tortoise-->13
tortoise-->14
tortoise-->15
tortoise-->16
tortoise-->17
tortoise-->18
tortoise-->19
tortoise-->20
tortoise-->21
tortoise-->22
tortoise-->23
tortoise-->24
tortoise-->25
tortoise-->26
tortoise-->27
tortoise-->28
tortoise-->29
tortoise-->30
tortoise-->31
tortoise-->32
tortoise-->33
tortoise-->34
tortoise-->35
tortoise-->36
tortoise-->37
tortoise-->38
tortoise-->39
tortoise-->40
tortoise-->41
tortoise-->42
tortoise-->43
tortoise-->44
tortoise-->45
tortoise-->46
tortoise-->47
tortoise-->48
tortoise-->49
tortoise-->50
tortoise-->51
tortoise-->52
tortoise-->53
tortoise-->54
tortoise-->55
tortoise-->56
tortoise-->57
tortoise-->58
tortoise-->59
tortoise-->60
tortoise-->61
tortoise-->62
tortoise-->63
tortoise-->64
tortoise-->65
tortoise-->66
tortoise-->67
tortoise-->68
tortoise-->69
tortoise-->70
tortoise-->71
tortoise-->72
tortoise-->73
tortoise-->74
tortoise-->75
tortoise-->76
tortoise-->77
tortoise-->78
tortoise-->79
tortoise-->80
tortoise-->81
tortoise-->82
tortoise-->83
tortoise-->84
tortoise-->85
tortoise-->86
tortoise-->87
tortoise-->88
tortoise-->89
tortoise-->90
tortoise-->91
tortoise-->92
tortoise-->93
tortoise-->94
tortoise-->95
tortoise-->96
tortoise-->97
tortoise-->98
tortoise-->99
tortoise-->100
winner-->tortoise
rabbit-->10

模拟龟兔赛跑(使用Callable)

代码如下:

package com.msl.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 模拟龟兔赛跑
 * @author Senley
 *
 */
public class CRacer implements Callable<Integer>{
    private String winner;//胜利者
    @Override
    public Integer call() throws Exception {
        for(int steps=1;steps<=100;steps++) {
            //模拟休息
            if(Thread.currentThread().getName().equals("pool-1-thread-1")&&steps%10==0) {
                Thread.sleep(100);
            }
            System.out.println(Thread.currentThread().getName()+"-->"+steps);
            //比赛是否结束
            boolean flag = gameOver(steps);
            if(flag) {
                return steps;
            }
        }
        return null;
    }
    
    private boolean gameOver(int steps) {
        if(winner!=null) {//存在胜利者
            return true;
        }else {
            if(steps==100) {
                winner = Thread.currentThread().getName();
                System.out.println("winner-->"+winner);
                return true;
            }
        }
        return false;
    }
    
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        CRacer racer = new CRacer();
        //创建执行服务: 
        ExecutorService  ser=Executors.newFixedThreadPool(2);
        //提交执行: 
        Future<Integer> result1 =ser.submit(racer) ;
        Future<Integer> result2 =ser.submit(racer) ;
        //获取结果:  
        Integer r1 =result1.get();
        Integer r2 =result2.get();
        System.out.println(r1+"-->"+r2);
        //关闭服务:  
        ser.shutdownNow();
    }
}

结果如下:

pool-1-thread-2-->1
pool-1-thread-1-->1
pool-1-thread-1-->2
pool-1-thread-1-->3
pool-1-thread-1-->4
pool-1-thread-1-->5
pool-1-thread-1-->6
pool-1-thread-1-->7
pool-1-thread-1-->8
pool-1-thread-1-->9
pool-1-thread-2-->2
pool-1-thread-2-->3
pool-1-thread-2-->4
pool-1-thread-2-->5
pool-1-thread-2-->6
pool-1-thread-2-->7
pool-1-thread-2-->8
pool-1-thread-2-->9
pool-1-thread-2-->10
pool-1-thread-2-->11
pool-1-thread-2-->12
pool-1-thread-2-->13
pool-1-thread-2-->14
pool-1-thread-2-->15
pool-1-thread-2-->16
pool-1-thread-2-->17
pool-1-thread-2-->18
pool-1-thread-2-->19
pool-1-thread-2-->20
pool-1-thread-2-->21
pool-1-thread-2-->22
pool-1-thread-2-->23
pool-1-thread-2-->24
pool-1-thread-2-->25
pool-1-thread-2-->26
pool-1-thread-2-->27
pool-1-thread-2-->28
pool-1-thread-2-->29
pool-1-thread-2-->30
pool-1-thread-2-->31
pool-1-thread-2-->32
pool-1-thread-2-->33
pool-1-thread-2-->34
pool-1-thread-2-->35
pool-1-thread-2-->36
pool-1-thread-2-->37
pool-1-thread-2-->38
pool-1-thread-2-->39
pool-1-thread-2-->40
pool-1-thread-2-->41
pool-1-thread-2-->42
pool-1-thread-2-->43
pool-1-thread-2-->44
pool-1-thread-2-->45
pool-1-thread-2-->46
pool-1-thread-2-->47
pool-1-thread-2-->48
pool-1-thread-2-->49
pool-1-thread-2-->50
pool-1-thread-2-->51
pool-1-thread-2-->52
pool-1-thread-2-->53
pool-1-thread-2-->54
pool-1-thread-2-->55
pool-1-thread-2-->56
pool-1-thread-2-->57
pool-1-thread-2-->58
pool-1-thread-2-->59
pool-1-thread-2-->60
pool-1-thread-2-->61
pool-1-thread-2-->62
pool-1-thread-2-->63
pool-1-thread-2-->64
pool-1-thread-2-->65
pool-1-thread-2-->66
pool-1-thread-2-->67
pool-1-thread-2-->68
pool-1-thread-2-->69
pool-1-thread-2-->70
pool-1-thread-2-->71
pool-1-thread-2-->72
pool-1-thread-2-->73
pool-1-thread-2-->74
pool-1-thread-2-->75
pool-1-thread-2-->76
pool-1-thread-2-->77
pool-1-thread-2-->78
pool-1-thread-2-->79
pool-1-thread-2-->80
pool-1-thread-2-->81
pool-1-thread-2-->82
pool-1-thread-2-->83
pool-1-thread-2-->84
pool-1-thread-2-->85
pool-1-thread-2-->86
pool-1-thread-2-->87
pool-1-thread-2-->88
pool-1-thread-2-->89
pool-1-thread-2-->90
pool-1-thread-2-->91
pool-1-thread-2-->92
pool-1-thread-2-->93
pool-1-thread-2-->94
pool-1-thread-2-->95
pool-1-thread-2-->96
pool-1-thread-2-->97
pool-1-thread-2-->98
pool-1-thread-2-->99
pool-1-thread-2-->100
winner-->pool-1-thread-2
pool-1-thread-1-->10
10-->100
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2021-2022 Senley
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信