创建线程

继承Thread 类


class PrimeThread extends Thread {
   

    @Override
    public void run() {
   
        for(int i = 0; i<20;i++){
   
            System.out.println("我是多线程"+i);
        }

    }

    public static void main(String[] args) {
   

        PrimeThread primeThread = new PrimeThread();
        primeThread.start();

        for(int i = 0; i<200;i++){
   
            System.out.println("我是主线程"+i);
        }
    }
}

执行这个主函数,那么结果是 主线程和多线程是交替执行


每次执行的结果是不一样的,因为多线程是cpu进行调度的,看CPU心情。

线程开启之后,不是立马执行,必须要CPU进行调度才可以执行

实现Runnable 接口

public class Runnablejk  implements Runnable {
   


    @Override
    public void run() {
   
        for(int i = 0; i<20;i++){
   
            System.out.println("我是多线程"+i);
        }
    }

    public static void main(String[] args) {
   

        Runnablejk primeThread = new Runnablejk();
        Thread thread = new Thread(primeThread);
        thread.start();

        for(int i = 0; i<200;i++){
   
            System.out.println("我是主线程"+i);
        }
    }
}

利用线程抢票

思路

票的个数是一个对象,也就是多个线程操作一个对象,这一个对象就是一个线程类里面的run方法,多个线程都要执行这一个线程类里面的run方法

代码

public class qp implements Runnable {
   
    private int ticket = 10;

    @Override
    public void run() {
   
        while (true){
   
            if(ticket <0){
   
               break;
            }
            try {
   
                Thread.sleep(Long.parseLong("200"));
            } catch (InterruptedException e) {
   
                e.printStackTrace();
            }

                System.out.println(Thread.currentThread().getName()+"拿到了第"+ticket--+"票");


        }

    }

    public static void main(String[] args) {
   
        qp qp = new qp();
        Thread thread = new Thread(qp,"小红");
        Thread thread1 = new Thread(qp,"需");
        Thread thread2 = new Thread(qp,"uu");
        thread.start();
        thread1.start();
        thread2.start();
    }
}

创建3个线程,对同一个线程类进行跑,这个线程类里面就是对一个数字进行相减

有问题


都拿到了第一票,这个肯定是不对的

这个就是多线程的并发问题,操作同一个资源的时候,就会有这个并发问题

实现callable接口

实现callable接口,重写call方法,方法是有返回值的,这个方法里面写线程要执行的东西

public class callablejk  implements Callable {
   


    @Override
    public Boolean call() throws Exception {
   
        for(int i = 0; i<20;i++){
   
            System.out.println("我是多线程"+i);
        }
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
   

        callablejk thread = new callablejk();
        callablejk thread1 = new callablejk();
        callablejk thread2 = new callablejk();

// 创建一个线程池
        ExecutorService executorService = Executors.newFixedThreadPool(3);
// 将我们创建的线程放到这个线程池里面,放到里面就可以执行了
        Future<Boolean> submit = executorService.submit(thread);
        Future<Boolean> submit1 = executorService.submit(thread1);
        Future<Boolean> submit2 = executorService.submit(thread2);
//获取执行的结果,不写也可以
        Boolean aBoolean = submit.get();
        Boolean aBoolean1 = submit1.get();
        Boolean aBoolean2 = submit2.get();

// 关闭服务
        executorService.shutdownNow();

    }
}