zoukankan      html  css  js  c++  java
  • 线程基础

    多线程基础(源于马士兵教育课程)

    1.什么是线程

    package com.mashibing.juc.c_000;
    import java.util.concurrent.TimeUnit;
    public class T01_WhatIsThread {
        private static class T1 extends Thread {
            @Override
            public void run() {
               for(int i=0; i<10; i++) {
                   try {
                       TimeUnit.MICROSECONDS.sleep(1);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
                   System.out.println("T1");
               }
            }
        }
        public static void main(String[] args) {
            //new T1().run();
            new T1().start();
            for(int i=0; i<10; i++) {
                try {
                    TimeUnit.MICROSECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("main");
            }
    
        }
    }
    
    • new T1().run();函数的简单调用,先执行输出10个T1,后输出10个main
    • new T1().start();开启一个新的线程,T1,main交替输出

    2.如何创建线程

    package com.mashibing.juc.c_000;
    public class T02_HowToCreateThread {
        static class MyThread extends Thread {
            @Override
            public void run() {
                System.out.println("Hello MyThread!");
            }
        }
        static class MyRun implements Runnable {
            @Override
            public void run() {
                System.out.println("Hello MyRun!");
            }
        }
        public static void main(String[] args) {
            new MyThread().start();
            new Thread(new MyRun()).start();
            new Thread(()->{
                System.out.println("Hello Lambda!");
            }).start();
        }
    }
    //请你告诉我启动线程的三种方式 1:Thread 2: Runnable 3:Executors.newCachedThrad
    
    • 继承Thread,重写run()方法
    • 实现Runnable接口,将对象作为参数传入new Thread()对象构造函数中
    • Thread对象中传入lambda表达式

    3.sleep,yield,join

    package com.mashibing.juc.c_000;
    
    public class T03_Sleep_Yield_Join {
        public static void main(String[] args) {
    //        testSleep();
    //        testYield();
            testJoin();
        }
        static void testSleep() {
            new Thread(()->{
                for(int i=0; i<100; i++) {
                    System.out.println("A" + i);
                    try {
                        Thread.sleep(500);
                        //TimeUnit.Milliseconds.sleep(500)
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        static void testYield() {
            new Thread(()->{
                for(int i=0; i<100; i++) {
                    System.out.println("A" + i);
                    if(i%10 == 0) Thread.yield();
    
    
                }
            }).start();
            new Thread(()->{
                for(int i=0; i<100; i++) {
                    System.out.println("------------B" + i);
                    if(i%10 == 0) Thread.yield();
                }
            }).start();
        }
        static void testJoin() {
            Thread t1 = new Thread(()->{
                for(int i=0; i<100; i++) {
                    System.out.println("A" + i);
                    try {
                        Thread.sleep(500);
                        //TimeUnit.Milliseconds.sleep(500)
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            Thread t2 = new Thread(()->{
    
                try {
                    t1.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                for(int i=0; i<100; i++) {
                    System.out.println("A" + i);
                    try {
                        Thread.sleep(500);
                        //TimeUnit.Milliseconds.sleep(500)
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
    
            t1.start();
            t2.start();
        }
    }
    
    • sleep,当前线程睡去,其他线程执行
    • yield,当前线程谦让的退出一下,进入等待队列等待执行
    • join,等待join的线程执行完才能继续执行,用于实现线程A需在线程B后执行这类要求

    4.线程状态

    package com.mashibing.juc.c_000;
    public class T04_ThreadState {
        static class MyThread extends Thread {
            @Override
            public void run() {
                System.out.println(this.getState());
                for(int i=0; i<10; i++) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(i);
                }
            }
        }
        public static void main(String[] args) {
            Thread t = new MyThread();
            System.out.println(t.getState());
            t.start();
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(t.getState());
        }
    }
    

    线程状态图

  • 相关阅读:
    深入浅出JSONP--解决ajax跨域问题
    Apache与Tomcat的区别
    项目终于接近尾声了
    交互设计[小插曲]--网站UI配色
    使用 Jasmine 进行测试驱动的 JavaScript 开发
    javascript单元测试
    MySQL查询当前数据库中所有记录不为空的表
    cannot be resolved to a type的错误
    oracle 表空数据导出dmp ,空表导出失败
    Iterable<E> Iterator<E>
  • 原文地址:https://www.cnblogs.com/fjf3997/p/13217544.html
Copyright © 2011-2022 走看看