线程常用方法,线程安全和同步锁

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()+"执行录屏....");
            }
            
        }
        
    }
}

猜你喜欢

转载自www.cnblogs.com/sunBinary/p/10540878.html