美文网首页
多线程系列02-实现多线程的方式

多线程系列02-实现多线程的方式

作者: Sandy_678f | 来源:发表于2019-07-17 15:38 被阅读0次

    常见的实现多线程的方式:Thread和Runnable
    其他方式:实现Callable接口通过Future/FutureTask包装器来创建Thread线程,通过线程池实现有返回结果的线程

    1. Runnable,Runnable是一个接口,仅包含了一个run()方法。
    @FunctionalInterface
    public interface Runnable {
        /**
         * When an object implementing interface <code>Runnable</code> is used
         * to create a thread, starting the thread causes the object's
         * <code>run</code> method to be called in that separately executing
         * thread.
         * <p>
         * The general contract of the method <code>run</code> is that it may
         * take any action whatsoever.
         *
         * @see     java.lang.Thread#run()
         */
        public abstract void run();
    }
    

    Runnable实例

    public class MyThread2 implements Runnable {
    
        private int ticket = 20;
        @Override
        public void run() {
            for(int i=0;i<30;i++){
                if(this.ticket>0){
                    System.out.println(Thread.currentThread().getName()+" 卖票:ticket"+this.ticket--);
                }
            }
        }
    
        public static void main(String[] args) {
    
            MyThread2 myThread2 = new MyThread2();
    
            //主线程main创建并启动3个子线程,而且这3个子线程都是基于“myThread2这个Runnable对象”而创建的。这3个子线程一共卖出了20张票。
            //这3个子线程可能会由于并发问题,导致卖出多余20张票
            Thread t1 = new Thread(myThread2);
            Thread t2 = new Thread(myThread2);
            Thread t3 = new Thread(myThread2);
    
            t1.start();
            t2.start();
            t3.start();
        }
    }
    

    运行结果

    Thread-0 卖票:ticket20 ——并发
    Thread-1 卖票:ticket20 
    Thread-2 卖票:ticket19
    Thread-1 卖票:ticket17
    Thread-0 卖票:ticket18
    Thread-1 卖票:ticket15
    Thread-2 卖票:ticket16
    Thread-1 卖票:ticket13
    Thread-1 卖票:ticket11
    Thread-1 卖票:ticket10
    Thread-1 卖票:ticket9
    Thread-0 卖票:ticket14
    Thread-1 卖票:ticket8
    Thread-2 卖票:ticket12
    Thread-1 卖票:ticket6
    Thread-0 卖票:ticket7
    Thread-1 卖票:ticket4
    Thread-0 卖票:ticket3
    Thread-0 卖票:ticket2
    Thread-2 卖票:ticket5
    Thread-1 卖票:ticket1
    

    结果说明:
    (01) 和下面“MyThread继承于Thread”不同;这里的MyThread实现了Runnable接口。
    (02) 主线程main创建并启动3个子线程,而且这3个子线程都是基于“myThread2这个Runnable对象”而创建的。运行结果是这3个子线程一共卖出了20张票。这说明它们是共享了MyThread接口的。
    (03) 因为并发的原因导致可能多余20张票。
    Runnable实现了资源的共享。即,多个线程都是基于某一个Runnable对象建立的,它们会共享Runnable对象上的资源。
    通常,建议通过“Runnable”实现多线程!

    1. Thread,Thread本身是实现了Runnable接口的类。
    public class MyThread1 extends Thread{
    
        private int ticket = 10;
        public void run(){
            for(int i=0; i<5; i++){
                if(this.ticket > 0){
                    System.out.println(this.getName()+" 卖票:ticket"+this.ticket--);
                }
            }
        }
    
        public static void main(String[] args) {
    
            //主线程main()创建了3个MyThread子线程,每个子线程都各自卖出5张票
            MyThread1 t1 = new MyThread1();
            MyThread1 t2 = new MyThread1();
            MyThread1 t3 = new MyThread1();
    
            t1.start();
            t2.start();
            t3.start();
        }
    }
    

    运行结果

    Thread-0 卖票:ticket10
    Thread-1 卖票:ticket10
    Thread-0 卖票:ticket9
    Thread-1 卖票:ticket9
    Thread-1 卖票:ticket8
    Thread-1 卖票:ticket7
    Thread-1 卖票:ticket6
    Thread-0 卖票:ticket8
    Thread-0 卖票:ticket7
    Thread-0 卖票:ticket6
    Thread-2 卖票:ticket10
    Thread-2 卖票:ticket9
    Thread-2 卖票:ticket8
    Thread-2 卖票:ticket7
    Thread-2 卖票:ticket6
    

    结果说明:
    (01) MyThread继承于Thread,它是自定义的线程。
    (02) 主线程main创建并启动3个MyThread子线程。每个子线程都各自卖出了10张票。

    相关文章

      网友评论

          本文标题:多线程系列02-实现多线程的方式

          本文链接:https://www.haomeiwen.com/subject/bcdxlctx.html