Java学习--基础--Thread线程

开启多线程操作方式1-------继承Thread(不推荐,避免单继承局限)

  1. 创建一个自由类继承Thread类

    public class TestThread01 extends Thread{}
    
  2. 重写run()方法

@Override
public void run() {
    //重写run方法
    for (int i = 0; i < 20; i++) {
        System.out.println("run"+i);
    }
    
    //super.run();
}
  1. 创建线程对象,使用start()方法开启线程
TestThread01 thread01 = new TestThread01();//创建线程对象
TestThread01 thread02 = new TestThread01();//创建线程对象
TestThread01 thread03 = new TestThread01();//创建线程对象
thread01.start(); //开启01线程
thread02.start(); //开启02线程
thread03.start(); //开启03线程

完整实例操作:

//1.首先自定义一个类继承Thread
//2.创建线程对象,然后重写线程的run()方法
//3.然后使用start()开启线程
public class TestThread01 extends Thread{

    @Override
    public void run() {
        //重写run方法
        for (int i = 0; i < 20; i++) {
            System.out.println("run"+i);
        }
        //super.run();
    }

    public static void main(String[] args) {

        TestThread01 thread01 = new TestThread01();//创建线程对象
        TestThread01 thread02 = new TestThread01();//创建线程对象
        TestThread01 thread03 = new TestThread01();//创建线程对象
        thread01.start(); //开启线程
        thread02.start();
        thread03.start();
        //main线程,主线程
        for (int i = 0; i < 20; i++) {
            System.out.println("学习多线程"+i);
        }
    }
    //总结:线程开启不一定立即执行,由CPU调度执行
}

开启多线程基础操作2-------实现Runnable接口

  1. 创建一个自由类实现Runnable接口
//使用接口实现多线程
public class TestThread02 implements Runnable {}
  1. 重写run();方法
//重写run()方法
@Override
public void run() {
    for (int i = 0; i < 20; i++) {

        System.out.println("实现Runnable方法实现多线程"+i);
    }
}
  1. 创建目标线程对象,把目标线程对象丢入Thread中启动start();
//创建线程对象
TestThread02 testThread01 = new TestThread02();
TestThread02 testThread02 = new TestThread02();
TestThread02 testThread03 = new TestThread02();

//Thread thread = new Thread(testThread02);
//thread.start();

new Thread(testThread01).start();//开启线程
new Thread(testThread02).start();//开启线程
new Thread(testThread03).start();//开启线程

完整实例操作:

//使用接口实现多线程
public class TestThread02 implements Runnable {

    //重写run()方法
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println("实现Runnable方法实现多线程"+i);
        }
    }
    public static void main(String[] args) {

        //创建线程对象
        TestThread02 testThread01 = new TestThread02();
        TestThread02 testThread02 = new TestThread02();
        TestThread02 testThread03 = new TestThread02();

        //Thread thread = new Thread(testThread02);
        //thread.start();

        new Thread(testThread01).start();//开启线程
        new Thread(testThread02).start();//开启线程
        new Thread(testThread03).start();//开启线程

    }
}

线程基本操作

启动多个线程操作同一个对象

发现------》线程不安全----数据紊乱

package thread;

//使用Runnable接口实现多个线程操作同一个对象
public class TestThread03 implements Runnable{

    private  int ticknum = 10;
    //重写run();方法
    @Override
    public void run() {

//        try {
//            Thread.sleep(500);//模拟延时
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        while (true){
            if (ticknum<=0){
                break;
            }
            System.out.println(Thread.currentThread().getName()+"拿到了第"+ticknum--+"张票");
        }
    }

    public static void main(String[] args) {
        TestThread03 testThread03 = new TestThread03();
        new Thread(testThread03,"小明").start();
        new Thread(testThread03,"小红").start();
        new Thread(testThread03,"小蓝").start();
    }
}

多线程模拟龟兔赛跑

程序执行双线程操作

//模拟龟兔赛跑
public class Race implements Runnable{

    //定义一个胜利者
    static String winner;

    //重写run();方法
    @Override
    public void run() {

        for (int i = 0; i <= 100; i++) {
            if (Thread.currentThread().getName().equals("兔子")&&i%15==0){
                try {
                    Thread.sleep(1);//模拟兔子睡觉
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            boolean flag = win(i);
            if (flag){
                break;
            }
            System.out.println(Thread.currentThread().getName()+"---》跑了"+i+"步");
        }
    }

    //判断胜利
    public boolean win(int steps){

        if (winner!=null){
            return true;
        }{
            //步数达到100 决出胜出者
            if (steps==100){
                winner = Thread.currentThread().getName();
                System.out.println("胜利者是"+winner);
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        //开启双线程
        Race race = new Race();
        new Thread(race,"兔子").start();
        new Thread(race,"乌龟").start();
    }

}

开启多线程基础操作3-------实现callable接口

操作注意:

  1. 实现callable接口,需要返回值类型
  2. 重写call()方法,需要抛出异常

操作步骤:

  1. 创建目标对象
  2. 创建执行服务:ExecutorService service = Executors.newFixedThreadPool(3);
  3. 提交执行:Future submit1 = service.submit(testThread01);
  4. 获取结果:String rs1 = submit1.get();
  5. 关闭服务:service.shutdown();

操作实例:

import java.util.concurrent.*;
//使用callable接口实现多线程操作
//1.创建类实现callable接口
//2.重写call()方法
//3.创建服务
//4.sumbit()方法提交执行
//5.关闭服务
public class TestThread04 implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println("重写了call方法");
        return null;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestThread04 testThread01 = new TestThread04();
        TestThread04 testThread02 = new TestThread04();
        TestThread04 testThread03 = new TestThread04();
        TestThread04 testThread04 = new TestThread04();

        //创建服务
        ExecutorService service = Executors.newFixedThreadPool(3);//线程数量自定义

        //submit()方法提交执行
        Future<String> submit1 = service.submit(testThread01);
        Future<String> submit2 = service.submit(testThread02);
        Future<String> submit3 = service.submit(testThread03);
        Future<String> submit4 = service.submit(testThread04);

        //获取结果
        String rs1 = submit1.get();
        String rs2 = submit2.get();
        String rs3 = submit3.get();
        String rs4 = submit4.get();
        
        //关闭服务
        service.shutdown();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值