• 69期-Java SE-013_多线程-2-001-002


    ### 线程调度
    
    - 线程休眠
    
    休眠是指让当前线程暂停执行,从运行状态进入阻塞状态,将 CPU 资源让给其他线程的一种调度方式。
    
    sleep()
    
    - 线程合并
    
    合并是指将指定的某个线程合并到当前线程中,将原本两个交替执行的线程改为顺序执行,即一个线程执行完毕之后再来执行第二个线程,通过调用线程的 join 方法来实现合并。
    
    假设有两个线程:线程A和线程B,线程A在执行到某个时间点时,调用线程B的join方法,则表示从当前的时间节点开始,CPU资源被线程B独占,线程A进入阻塞状态,等待获取CPU资源进入运行状态。
    
    - 线程礼让
    
    在某一个时间节点,线程暂停争夺CPU资源,进入阻塞状态,但只是暂时的一瞬间,过后线程对象再次进入就绪状态来争夺CPU资源,yield 实现线程礼让。
    
    - 线程中断
    
    1、线程执行完毕之后自动停止。
    
    2、线程在执行过程中出现错误停止。
    
    3、线程在执行过程中手动停止。
    
     - public void stop()
     - public void interrupt()
     - public boolean isInterrupt()
    
    ### 线程同步
    
    可以通过 synchronized 修饰方法来实现线程同步,每个 Java 对象都有一个内置锁,内置锁会保护使用 synchronized 关键字修饰的方法,要调用该方法就必须先获得内置锁,否则就处于阻塞状态。
    
    ### 线程安全的单例模式
    
    单例是一种常见的软件设计模式,其核心思想是一个类只能有一个实例对象,由多个线程来共享该实例对象的资源。
    
    ```java
    public class SingletonDemo {
        private static SingletonDemo instance;
        private SingletonDemo() {
            System.out.println("创建单例对象");
        }
        public static SingletonDemo getInstance() {
            synchronized (SingletonDemo.class) {
                if(instance == null) {
                    instance = new SingletonDemo();
                }
            }
            return instance;
        }
    }
    ```

    Account.java

    public class Account implements Runnable {
        private static int num;
        @Override
        public synchronized void run() {
            // TODO Auto-generated method stub
            try {
                Thread.currentThread().sleep(1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            num++;
            System.out.println(Thread.currentThread().getName()+"是当前第"+num+"位访客");
        }
        
    }

    JoinRunnable.java

    public class JoinRunnable implements Runnable {
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            for(int i = 0; i < 200;i++) {
                System.out.println(i+"---------JoinRunnable");
            }
        }
    
    }

    MyRunnable.java

    public class MyRunnable implements Runnable {
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            for(int i = 0; i < 100; i++) {
                try {
                    Thread.currentThread().sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("------MyRunnable");
            }
        }
    
    }

    MyThread.java

    public class MyThread extends Thread {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            for(int i = 0; i < 100;i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("------Thread");
            }
        }
    }

    SingletonDemo.java

    public class SingletonDemo {
        private static SingletonDemo instance;
        private SingletonDemo() {
            System.out.println("创建单例对象");
        }
        public static SingletonDemo getInstance() {
            synchronized (SingletonDemo.class) {
                if(instance == null) {
                    instance = new SingletonDemo();
                }
            }
            return instance;
        }
    }

    SynchronizedTest.java

    public class SynchronizedTest {
        public static void main(String[] args) {
            for(int i = 0;i<5;i++) {
                Thread thread = new Thread(new Runnable() {
                    
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        SynchronizedTest synchronizedTest = new SynchronizedTest();
                        synchronizedTest.test();
                    }
                });
                thread.start();
            }
        }
        
        public void test() {
            synchronized (SynchronizedTest.class) {
                System.out.println("start...");
                try {
                    Thread.currentThread().sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("end...");
            }
        }
    }

     Test.java

    public class Test {
        public static void main(String[] args) {
    //        MyRunnable myRunnable = new MyRunnable();
    //        Thread thread = new Thread(myRunnable);
    //        thread.start();
            MyThread myThread = new MyThread();
            myThread.start();
        }
    }

    Test2.java

    public class Test2 {
        public static void main(String[] args) {
            JoinRunnable joinRunnable = new JoinRunnable();
            Thread thread = new Thread(joinRunnable);
            thread.start();
            for(int i = 0;i<500;i++) {
                if(i == 10) {
                    try {
                        thread.join(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                System.out.println(i+"+++++++++main");
            }
        }
    }

    Test3.java

    public class Test3 {
        public static void main(String[] args) {
            YieldThread1 thread1 = new YieldThread1();
            thread1.setName("Thread1");
            YieldThread2 thread2 = new YieldThread2();
            thread2.setName("Thread2");
            thread1.start();
            thread2.start();
        }
    }

    Test4.java

    public class Test4 {
        public static void main(String[] args) {
            Thread thread = new Thread(new Runnable() {
                
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    for(int i = 0; i < 10;i++) {
                        if(i == 5) {
                            Thread.currentThread().interrupt();
                        }
                        System.out.println("------Test");
                    }
                }
            });
            thread.start();
            System.out.println(thread.getState());
            System.out.println(thread.isInterrupted());
            System.out.println(thread.getState());
        }
    }

    Test5.java

    public class Test5 {
        public static void main(String[] args) {
            Account account = new Account();
            Thread t1 = new Thread(account,"线程A");
            Thread t2 = new Thread(account,"线程B");
            t1.start();
            t2.start();
            
        }
    }

    Test6.java

    public class Test6 {
        public static void main(String[] args) {
            Thread thread = new Thread(new Runnable() {
                
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    SingletonDemo singletonDemo = SingletonDemo.getInstance();
                }
            });
            thread.start();
            Thread thread2 = new Thread(new Runnable() {
                
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    SingletonDemo singletonDemo = SingletonDemo.getInstance();
                }
            });
            thread2.start();
        }
    }

     YieldThread1.java

    public class YieldThread1 extends Thread {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            for(int i = 0; i < 10; i++) {
                if(i == 5) {
                    yield();
                }
                System.out.println(getName()+"------"+i);
            }
        }
    }

    YieldThread2.java

    public class YieldThread2 extends Thread {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            for(int i = 0; i < 10;i++) {
                System.out.println(getName()+"------"+i);
            }
        }
    }
  • 相关阅读:
    matlab基本数据结构struct
    matlab基本函数strcmp num2str 字符串格式
    matlab基本函数inf, isempty, round, floor, fix
    matlab基本函数randperm end数组索引
    matlab基本函数find
    【未通过】LintCode #366 斐波纳契数列
    LintCode #3 统计数字
    计算1至n中数字X出现的次数
    LintCode #2 尾部的零
    LintCode #1 A + B 问题
  • 原文地址:https://www.cnblogs.com/HiJackykun/p/11172817.html
Copyright © 2020-2023  润新知