多线程

作者: 李建彪 | 来源:发表于2017-06-01 11:11 被阅读0次
    public class Main {
        static int i = 0;
        public static void main(String[] args) {
            //启动线程
            new mThread().start();
            new mThread().start();
            //主线程中循环
            for (; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + "->" + i);
            }
        }
    }
    class mThread extends Thread {
        int i = 0;
        @Override
        public void run() {
            super.run();
            for (; i < 100; i++) {
                System.out.println(getName() + "->" + i);
            }
        }
    }
    
    
    public class Main {
        static int i = 0;
        public static void main(String[] args) {
            //启动线程
            mThread mThread = new mThread();
            new Thread(mThread).start();
            new Thread(mThread).start();        
            //主线程中循环
            for (; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + "->" + i);
            }
        }
    }
    class mThread implements Runnable {
        int i = 0;
        @Override
        public void run() {
            for (; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + "->" + i);
            }
        }
    }
    
    

    注意: 上面代码中的Thread 公用的一个Runnable Target 所以变量i只有一份 打印的时候 两个线程公用的是一个变量i,两个线程 不共用一个线程方法就是 创建两个Runnable Target 这样就会创建两个变量i线程和线程之间就没有联系了

    public class Main {
        static int i = 0;
        public static void main(String[] args) {
            //启动线程
            new Thread(new mThread()).start();
            new Thread(new mThread()).start();      
            //主线程中循环
            for (; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + "->" + i);
            }
        }
    }
    class mThread implements Runnable {
        int i = 0;
        @Override
        public void run() {
            for (; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + "->" + i);
            }
        }
    }
    
    public class Main {
        public static void main(String[] args) throws InterruptedException, ExecutionException {
            Main main = new Main();
            // 创建线程,创建带返回值的Callable线程
            FutureTask<Integer> task = new FutureTask<>((Callable<Integer>) () -> {
                int i = 0;
                for (; i < 100; i++) {
                    System.out.println(Thread.currentThread().getName() + "->" + i);
                }
                return i;
            });
            // 启动线程
            new Thread(task).start();
            // 主线程
            for (int i = 0; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + "->" + i);
            }
            System.out.println("子线程的返回值为:"+task.get());
        }
    }
    
    线程状态图
    public class Main {
        public static void main(String[] args) throws InterruptedException, ExecutionException {
            // 创建线程,创建带返回值的Callable线程
            FutureTask<Integer> task = new FutureTask<>((Callable<Integer>) () -> {
                int i = 0;
                for (; i < 100; i++) {
                    System.out.println(Thread.currentThread().getName() + "->" + i);
                }
                return i;
            });
            // 创建线程空间
            Thread thread = new Thread(task);
            // 主线程
            for (int i = 0; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + "->" + i);
                if(i == 20){
                    //启动线程
                    thread.start();
                    //让当前线程等待
                    thread.join();
                }
            }
            System.out.println("子线程的返回值为:"+task.get());
        }
    }
    
    public class Main {
        public static void main(String[] args) throws InterruptedException {
            for(int i = 0;i <=100;i++){
                System.out.println(Thread.currentThread().getName() + "->" + i);
                if(i==20){
                    mThread thread = new mThread();
                    mThread thread1 = new mThread();
                    thread.start();
                    thread1.start();
                    thread.join();
                }
            }
        }
    }
    
    class mThread extends Thread {
        int i = 0;
        @Override
        public void run() {
            super.run();
            for (; i < 100; i++) {
                System.out.println(getName() + "->" + i);
            }
        }
    }
    

    小结:如果在主线程中创建两个线程(thread ,thread 1)然后在主线程执行thread的join方法,会让主线程等待thread执行完成后才会获得调度。期间 thread和thread1会共同执行。

    public class Main {
        public static void main(String[] args) throws InterruptedException {
            for(int i = 0;i <=100;i++){
                System.out.println(Thread.currentThread().getName() + "->" + i);
                if(i == 20){
                    mThread thread = new mThread();
                    //设置为守护线程,主线程执行完成后 不需要等待子线程执行完就可以退出虚拟机
                    thread.setDaemon(true);
                    thread.start();}}}}
    class mThread extends Thread {
        int i = 0;
        @Override
        public void run() {
            super.run();
            for (; i < 800; i++) {
                System.out.println(getName() + "->" + i);}}}
    
    public class Main {
        public static void main(String[] args) throws InterruptedException {
            for (int i = 0; i <= 100; i++) {
                System.out.println(Thread.currentThread().getName() + "->" + i);
                if (i == 20) {
                    mThread thread = new mThread();
                    thread.start();
                }
                if(i == 50){
                    Thread.sleep(5000);
                }
            }
        }
    }
    class mThread extends Thread {
        int i = 0;
        @Override
        public void run() {
            super.run();
            for (; i < 1000; i++) {
                try {sleep(1);} catch (InterruptedException e) {}
                System.out.println(getName() + "->" + i);}}}
    
    public class Main {
        public static void main(String[] args) throws InterruptedException {
            mThread thread = new mThread();
            mThread thread1 = new mThread();
            thread.setPriority(Thread.MAX_PRIORITY);
            thread1.setPriority(Thread.MIN_PRIORITY);
            thread.start();
            thread1.start();
        }
    }
    
    class mThread extends Thread {
        int i = 0;
        @Override
        public void run() {
            super.run();
            for (; i < 100; i++) {
                try {
                    sleep(1);
                } catch (InterruptedException e) {
                }
                System.out.println(getName() + "->" + i);
                // 线程让步
                if (i == 20) {
                    yield();
                }
            }
        }
    }
    


    先看代码

    public class Main {
        public static void main(String[] args) throws InterruptedException {
            mThread thread = new mThread();
            mThread thread1 = new mThread();
            mThread thread2 = new mThread();
            mThread thread3 = new mThread();
            thread.start();
            thread1.start();
            thread2.start();
            thread3.start();
        }
    }
    
    class mThread extends Thread {
        static int i = 100;
        @Override
        public void run() {
            super.run();
            try {
                sleep(10);
            } catch (InterruptedException e) {
            }
            for (; i > 0;--i) {
                System.out.println(getName() + "->" + i);
            }
        }
    }
    

    上面代码正常来说只会出现一个100 可是大多数会出现多个100,这显然是不是我们想要的。

    public class Main {
        public static void main(String[] args) throws InterruptedException {
            new mThread().start();
            new mThread().start();
            new mThread().start();
            new mThread().start();
            new mThread().start();
            new mThread().start();
            new mThread().start();
            new mThread().start();
            new mThread().start();
            new mThread().start();
            new mThread().start();
        }
    }
    
    class mThread extends Thread {
        static Integer i = 1000;
        @Override
        public void run() {
            super.run();
            try {
                sleep(10);
            } catch (InterruptedException e) {
            }
            synchronized(java.util.Calendar.getInstance()){
                for (; i > 0;--i) {
                    System.out.println(getName() + "->" + i);
                }
            }
        }
    }
    

    同步方法:

    BlockingQuaue实现类和父类关系图 线程池工具类类图

    相关文章

      网友评论

          本文标题:多线程

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