zoukankan      html  css  js  c++  java
  • 线程常用方法,线程安全和同步锁

    join
    package com.thread.demo.base;
    /**
     * join方法的使用:
     * 作用: 让其主线程等待子线程完成后再执行
     * @author Administrator
     *
     */
    public class ThreadJoin {
        
        public static void main(String[] args) {
            System.out.println("main thread start");
            Thread t1 = new Thread(new MyRunnable(),"AAA");
            Thread t2 = new Thread(new MyRunnable(),"BBB");
            t1.start();
            t2.start();
            try {
                t1.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            System.out.println("main thread end");
        }
    
        static class MyRunnable implements Runnable {
            public void run() {
                for (int i = 0; i< 5; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"执行中.....");
                }
                
            }
            
        }
    }
    守护线程
    package com.thread.demo.base;
    
    import java.io.IOException;
    
    /**
     * 测试守护线程
     * 作用: 为其非守护线程提供服务
     * @author Administrator
     *
     */
    public class ThreadDaemon {
        
        public static void main(String[] args) {
            System.out.println("mian Thread start");
            Thread t1 = new Thread(new MyRunnable(),"AAA");
            t1.setDaemon(true); // 设置守护线程
            t1.start();
            
            Thread t2 = new Thread(new MyRunnable1(),"BBB");
            t2.start();
            
            /*try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
            // I/O阻塞
            try {
                System.out.println("请输入内容:");
                System.in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("main thread end!");
        }
        
        static class MyRunnable implements Runnable {
            public void run() {
                while(true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"执行GC操作准备....");
                }
                
            }
            
        }
        
        static class MyRunnable1 implements Runnable {
            public void run() {
                for(int i = 0; i < 5; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"执行录屏....");
                }
                
            }
            
        }
    }

     线程礼让

    package com.thread.demo.base;
    
    /**
     * 线程礼让
     * @author Administrator
     *
     */
    public class ThreadYield {
        
        public static void main(String[] args) {
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    for ( int i = 0; i < 5; i++) {
                        System.out.println(Thread.currentThread().getName()+"执行"+i);
                        
                    }
                }
            },"AAA");
            
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    for ( int i = 0; i < 5; i++) {
                        System.out.println(Thread.currentThread().getName()+"执行"+i);
                        if (i==0) {
                            System.out.println("t1礼让了t2执行");
                            t1.yield();
                        }
                        
                    }
                }
            },"BBB");
            
            // 启动线程
            t1.start();
            t2.start();
            
            // t1让t2执行
        }
    }
    线程优化级
    package com.thread.demo.base;
    
    /**
     * 线程优化级: 提供线程优于其他线程执行的几率
     * @author Administrator
     *
     */
    public class ThreadPriority {
        
        public static void main(String[] args) {
            // 创建2个线程
            Thread t1 = new Thread(new MyRunnable(),"AAA");
            Thread t2 = new Thread(new MyRunnable1(),"BBB");
            
            // 设置线程优化级
            t1.setPriority(Thread.MIN_PRIORITY);
            t2.setPriority(Thread.MAX_PRIORITY);
            
            // 启动线程
            t1.start();
            t2.start();
            
        }
        
        static class MyRunnable implements Runnable {
            public void run() {
                for(int i = 0; i < 5; i++)  {
                    /*try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }*/
                    System.out.println(Thread.currentThread().getName()+"执行GC操作准备....");
                }
                
            }
            
        }
        
        static class MyRunnable1 implements Runnable {
            public void run() {
                for(int i = 0; i < 5; i++) {
                    /*try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }*/
                    System.out.println(Thread.currentThread().getName()+"执行录屏....");
                }
                
            }
            
        }
    }

     线程安全问题

    package com.thread.demo.safe;
    /**
     * 线程安全问题: 多个线程共享同一个资源导致
     * @author Administrator
     *
     */
    public class ThreadSafe1 {
        
        public static void main(String[] args) {
            // 创建3个卖票窗口
            MyThread t1 = new MyThread("窗口1");
            MyThread t2 = new MyThread("窗口2");
            MyThread t3 = new MyThread("窗口3");
            
            t1.start();
            t2.start();
            t3.start();
        }
        
        static class MyThread extends Thread {
            
            private static int count = 10;
            
            public MyThread(String name) {
                super(name);
            }
            
            
            @Override
            public void run() {
                while(count > 0) {
                    String tname = Thread.currentThread().getName();
                    System.out.println(tname+count);
                    // 1,2 count = 1
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    tname = Thread.currentThread().getName();
                    System.out.println(tname+"卖出了"+(count--)+"票");
                }
            }
        }
    }
    package com.thread.demo.safe;
    /**
     * 线程安全问题: 多个线程共享同一个资源导致
     * @author Administrator
     *
     */
    public class ThreadSafe2 {
    
        private static int count;
        
        public static void main(String[] args) {
            Thread t1 = new Thread(new MyRunnable());
            Thread t2 = new Thread(new MyRunnable());
            
            t1.start();
            t2.start();
            
            try {
                t1.join();
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            System.out.println("count="+count);
            
        }
        
        static class MyRunnable implements Runnable {
            @Override
            public void run() {
                for (int i = 0; i < 10000000; i++) {
                    count++;
                }
            }
        }
    }
    同步块
    package com.thread.demo.safe.sync;
    
    /**
     * 使用同步块解决线程安全的问题
     * @author Administrator
     *
     */
    public class SyncBlock {
        
        private static SyncBlock lock = new SyncBlock();
        private static int count;
        
        public static void main(String[] args) {
            Thread t1 = new Thread(new MyRunnable());
            Thread t2 = new Thread(new MyRunnable());
            
            t1.start();
            t2.start();
            
            try {
                t1.join();
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            System.out.println("count="+count);
            
        }
        
        static class MyRunnable implements Runnable {
            @Override
            public void run() {
                for (int i = 0; i < 10000000; i++) {
                    // synchronized 保证在同一个时间段中不能有其他线程同时执行
                    synchronized(lock) {
                        count++;
                    }
                }
            }
        }
    }
    同步关键字
    package com.thread.demo.safe.sync;
    
    /**
     * 使用同步关键字加锁对象的实例方法
     * @author Administrator
     *
     */
    public class SyncInstance {
        private static int count;
        
        public static void main(String[] args) {
            MyRunnable target = new MyRunnable();
            Thread t1 = new Thread(target);
            Thread t2 = new Thread(target);
            
            t1.start();
            t2.start();
            
            try {
                t1.join();
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            System.out.println("count="+count);
            
        }
        
        static class MyRunnable implements Runnable {
            
            /**
             * 对count操作的实例方法
             */
            public synchronized void increse() {
                count++;
            }
            
            @Override
            public void run() {
                for (int i = 0; i < 10000000; i++) {
                    increse();
                }
            }
        }
    }
    Class类锁
    package com.thread.demo.safe.sync;
    
    /**
     * 使用同步关键字加锁Class对象
     * @author Administrator
     *
     */
    public class SyncClass {
        private static int count;
        
        public static void main(String[] args) {
            Thread t1 = new Thread(new MyRunnable());
            Thread t2 = new Thread(new MyRunnable());
            
            t1.start();
            t2.start();
            
            try {
                t1.join();
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            System.out.println("count="+count);
            
        }
        
        static class MyRunnable implements Runnable {
            
            /**
             * 对count操作的类方法
             */
            public static synchronized void increse() {
                count++;
            }
            
            @Override
            public void run() {
                for (int i = 0; i < 10000000; i++) {
                    increse();
                }
            }
        }
    }
  • 相关阅读:
    身份证相关类
    微信开发相关文档
    password、文件MD5加密,passwordsha256、sha384、sha512Hex等加密
    图的割点(边表集实现)
    动态库DLL中类的使用
    吴恩达机器学习笔记_第三周
    Android官方开发文档Training系列课程中文版:性能优化建议
    简单算法汇总
    Gson解析第三方提供Json数据(天气预报,新闻等)
    Java字节码 小结
  • 原文地址:https://www.cnblogs.com/sunBinary/p/10540878.html
Copyright © 2011-2022 走看看