多线程

作者: bfx1000 | 来源:发表于2018-08-27 11:04 被阅读0次

    Reference:http://www.java1234.com

    多线程实现的两种方式:

    方法1、实现Runnable接口(推荐使用)
    方法2、继承Thread类
    实例:张三、李四一起吃包子,每人吃了十个(两人吃包子同时进行)

    方法1、实现Runnable接口

    分以下几个步骤:

    1. 重写run()方法
    Thread t1 =new Thread( new runnable("张三"));
    t1.start();
    

    写测试文件TestRunnable.java

    import java.lang.Thread; 
    
    class TestRunnable implements Runnable{
        private int baozi=1;
        private String threadName;
    
        private TestRunnable(String threadName){
            super();
            this.threadName=threadName;
        }
        @Override
        public void run(){
            while(baozi<11){
                System.out.println(threadName+"吃第"+baozi+"个包子");
                baozi++;
            }
            
        }
        public static void main (String[] args){
            System.out.println("张三、李四一起吃包子,每人吃十个");
            TestRunnable t11=new TestRunnable("张三");
            Thread t1 =new Thread(t11);
            t1.start();
    
            TestRunnable t22=new TestRunnable("lisi");
            Thread t2 =new Thread(t22);
            t2.start();
        }
    }
    

    方法2、继承Thread

    import java.lang.Thread;
    class TestThread2 extends Thread{
        private int baozi =1;
        private String threadName;
    
        private TestThread2(String threadName){
            super();
            this.threadName=threadName;
        }
        @Override
        public void run(){
            while(baozi<11){
                System.out.println(threadName+" eat baozi's number is "+baozi);
                baozi++;
            }
        }
        public static void main(String[] args) {
            TestThread2 t1=new TestThread2("zhangsan");
            t1.start();
    
            TestThread2 t2=new TestThread2("lisi");
            t2.start();
    
        }
    }
    

    第二个实例:
    超级张三开三个线程一起吃10个包子

    class TestThread3 implements Runnable{
        /**
         * 超级张三吃10个包子,每次吃三个
         */
    
        private int baozi =1;
        private String threadName;
    
        private TestThread3(String threadName){
            super();
            this.threadName=threadName;
        }
        // synchronized 作用是只能有一个线程进入该方法
        @Override
        public synchronized void run(){
            while(baozi<11){
                System.out.println(threadName+" eat baozi's number is "+baozi);
                baozi++;
            }
        }
        public static void main(String[] args) {
            TestThread3 t1=new TestThread3("超级张三");
    
            // 对t1开启三个线程,三个“超级张三”一起吃十个包子
            // 每个线程一次吃一个包子
            Thread t11=new Thread(t1);
            Thread t12=new Thread(t1);
            Thread t13=new Thread(t1);
            t11.start();
            t12.start();
            t13.start();
        }
    }
    
    

    结果图:


    线程状态图:

    状态 解释
    新建状态 Thread t=new Thread()后,对象处于新建状态,但不可运行
    就绪状态 新建对象后,调用start()方法可以启动线程
    运行状态 线程启动后,自动调用重写的run()方法
    堵塞状态 正在运行的某个线程在被人为挂起或者需要进行输入输出/操作时,让出cup并暂停自己的执行并进入阻塞状态。阻塞状态不能进入列队,当阻塞原因被消除后,线程可以转入就绪状态
    死亡状态 调用stop()方法或者run()结束后,处于死亡状态

    线程的常用方法:

    相关文章

      网友评论

          本文标题:多线程

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