• 多线程常见的3中实现方式


    1.继承Thread类

    源代码:

    package com.zy.test.www.multiThread;
    /**
     * 多线程实现方式1:继承Thread类
     * @author zy
     */
    public class ByExtendsThread extends Thread{    
        
        public ByExtendsThread(String name) {
            super(name);
        }
    
        @Override
        public void run() {
            System.out.println(getName() + " 线程运行开始!");
            for (int i = 1; i <= 5; i++) {
                System.out.println(getName() + " " + i);
                try {
                    sleep((int) Math.random() * 10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(getName() + " 线程运行结束!");
        }
        
        public static void main(String[] args) {
            System.out.println(Thread.currentThread().getName() + " 线程运行开始!");
            new ByExtendsThread("A").start();
            new ByExtendsThread("B").start();
            System.out.println(Thread.currentThread().getName() + " 线程运行结束!");
        }
    
    }

     运行效果:

    main 线程运行开始!
    A 线程运行开始!
    main 线程运行结束!
    A 1
    B 线程运行开始!
    B 1
    A 2
    B 2
    A 3
    B 3
    A 4
    B 4
    A 5
    B 5
    A 线程运行结束!
    B 线程运行结束!

     2.实现Runnable接口

    源代码:

    package com.zy.test.www.multiThread;
    
    /**
     * 多线程实现方式2:实现Runnable接口
     * @author zy
     */
    public class ByImplementsRunnable implements Runnable{    
    
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " 线程运行开始!");
            for (int i = 1; i <= 5; i++) {
                System.out.println(Thread.currentThread().getName() + " " + i);
                try {
                    Thread.sleep((int) Math.random() * 10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + " 线程运行结束!");
        }
        
        public static void main(String[] args) {
            System.out.println(Thread.currentThread().getName() + " 线程运行开始!");
            ByImplementsRunnable byImplementsRunnable = new ByImplementsRunnable();
            new Thread(byImplementsRunnable, "A").start();
            new Thread(byImplementsRunnable, "B").start();
            System.out.println(Thread.currentThread().getName() + " 线程运行结束!"); 
    } }

      运行效果:

    main 线程运行开始!
    A 线程运行开始!
    main 线程运行结束!
    A 1
    B 线程运行开始!
    B 1
    A 2
    B 2
    A 3
    B 3
    A 4
    B 4
    A 5
    B 5
    A 线程运行结束!
    B 线程运行结束!

     3.使用线程池

    源代码:

    package com.sunny.www.multiThreads.realize;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    /**
     * 多线程实现方式3:线程池
     * @author zy
     */
    public class ByThreadPool {
        private Long ticketCount;    //总门票数量    
        
        public ByThreadPool(Long ticketCount) {
            super();
            this.ticketCount = ticketCount;
        }
    
        public Long getTicketCount() {
            return ticketCount;
        }
    
        public void setTicketCount(Long ticketCount) {
            this.ticketCount = ticketCount;
        }
    
        public static void main(String[] args) throws InterruptedException {
            ByThreadPool byThreadPool = new ByThreadPool(Long.valueOf(Integer.MAX_VALUE));
            int poolSize = 10;    //线程池大小
            ExecutorService pool = Executors.newFixedThreadPool(poolSize);
            long start = System.currentTimeMillis();
            for(int i = 0; i < poolSize; i++){
                pool.execute(new TicketWindow(byThreadPool));
            }
            pool.shutdown();        
            pool.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
            long end = System.currentTimeMillis();
            System.out.println("all thread complete,spend times=" + (end-start) + "ms");
            
        }
    
    }
    
    class TicketWindow implements Runnable {
        private ByThreadPool byThreadPool;
        
        public TicketWindow(ByThreadPool byThreadPool) {
            super();
            this.byThreadPool = byThreadPool;
        }
    
        @Override
        public void run() {
            saleTicket();
            
        }
    
        private synchronized void saleTicket() {        
            Long ticketCount = byThreadPool.getTicketCount();
            while(0 < ticketCount){
                System.out.println(Thread.currentThread().getName() + " , sale " + ticketCount--);
            }        
        }    
    }
     运行效果:

    1 pool-1-thread-3 , sale 100
    2 pool-1-thread-1 , sale 100
    3 pool-1-thread-1 , sale 99
    4 pool-1-thread-1 , sale 98
    5 pool-1-thread-1 , sale 97
    6 pool-1-thread-1 , sale 96
    7 pool-1-thread-1 , sale 95
    8 。。。。
    View Code
  • 相关阅读:
    SOA精华的内容和实用的知识
    众多SEO专家集体盛赞
    黑客大曝光:VoIP安全机密与解决方案
    博文视点大讲堂41期SEO难点之网站内部链接结构
    TransactSQL管理与开发实例精粹
    千万不要错过云计算兴起的时代
    《海量数据库解决方案》之位图索引的结构和特征
    Oracle开发艺术
    Android应用程序的开发
    BizTalk Accelerator for HL7医疗行业消息路由处理机制
  • 原文地址:https://www.cnblogs.com/sunny08/p/4866137.html
Copyright © 2020-2023  润新知