多线程----锁,中断,线程组,设置线程优先级

1.加锁处理线程安全问题01

package 多线程;

import java.util.concurrent.locks.Lock;

public class 加锁解决线程安全问题 {
    public static void main(String[] args) {
        ThreadRunnable runnable = new ThreadRunnable();

        Thread thread1 = new Thread(runnable,"售票点1");
        Thread thread2 = new Thread(runnable,"售票点2");
        Thread thread3 = new Thread(runnable,"售票点3");
        Thread thread4 = new Thread(runnable,"售票点4");

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }
}
//用extends实现时 注意将锁设置成static  为共有的

class ThreadRunnable implements Runnable{
    private int count = 100;
    private Object lock = new Object();//可以使用任何引用对象来声明 没有其他作用
    @Override
    public void run() {
        while(true){
       synchronized (lock){//lock也可以为this
           //第一个线程来了锁住了并拿到钥匙  当第二个线程来的时候只能等待  因为它拿不到钥匙
               if (count!=0){
                   System.out.println(Thread.currentThread().getName()+"卖出第"+count+"票");
                   count--;
               }else{
                   break;
               }
           }

           try{
               java.lang.Thread.sleep(10);
           }catch(Exception e){
               System.out.println(e.getMessage());
           }
       }
    }

}

2.加锁处理线程安全问题02

package 多线程;

public class 加锁解决线程安全2 {
        public static void main(String[] args) {
            多线程.ThreadRunnable runnable = new 多线程.ThreadRunnable();

            Thread thread1 = new Thread(runnable,"售票点1");
            Thread thread2 = new Thread(runnable,"售票点2");
            Thread thread3 = new Thread(runnable,"售票点3");
            Thread thread4 = new Thread(runnable,"售票点4");

            thread1.start();
            thread2.start();
            thread3.start();
            thread4.start();
        }
    }
//用extends实现时 注意将锁设置成static  为共有的

    class ThreadRunnable2 implements Runnable{
        private int count = 100;
        @Override
        public void run() {
            while(count>0){

                //将可能出现线程安全的封装到一个方法中,然后调用此加锁的方法
                sellTicket();

                try{
                    java.lang.Thread.sleep(10);
                }catch(Exception e){
                    System.out.println(e.getMessage());
                }
            }
        }
        public synchronized void sellTicket(){
            if (count>0){
                System.out.println(Thread.currentThread().getName()+"卖出第"+count+"张票");
                count--;
            }
        }

    }

3.加锁处理线程安全问题03(锁对象)

package 多线程;

import java.util.concurrent.locks.ReentrantLock;

public class 加专门的锁对象 {
    public static void main(String[] args) {
        多线程.ThreadRunnable runnable = new 多线程.ThreadRunnable();

        Thread thread1 = new Thread(runnable,"售票点1");
        Thread thread2 = new Thread(runnable,"售票点2");
        Thread thread3 = new Thread(runnable,"售票点3");
        Thread thread4 = new Thread(runnable,"售票点4");

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }
}
//用extends实现时 注意将锁设置成static  为共有的

class ThreadRunnable3 implements Runnable{
    private int count = 100;
    private ReentrantLock reentrantLock = new ReentrantLock();
    @Override
    public void run() {
        while(count>0){
            reentrantLock.lock();//加锁、
           try{//确保万一出现异常解锁失败
               if (count>0){
                   System.out.println(Thread.currentThread().getName()+"卖出第"+count+"张票");
                   count--;
               }
           }catch(Exception e){
               System.out.println(e.getMessage());
           }
            reentrantLock.unlock();//解锁

            try{
                java.lang.Thread.sleep(10);
            }catch(Exception e){
                System.out.println(e.getMessage());
            }
        }
    }
}

4.线程的中断

package 多线程;
/*
Thread.stop() 直接杀死  一枪崩掉
Thread.interrupt()  将状态设置成中断 然后自己处理关闭杀死   自己处理杀死自己 可以留下遗言
 */
public class 线程的中断 {
    public static void main(String[] args) {
        Threads a = new Threads();
        a.start();
        //a.stop();
        try{
            Thread.sleep(5);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
        a.interrupt();
    }

}

class Threads extends Thread{
    @Override
    public void run() {

        for (int i = 0; i < 1000; i++) {
            if (interrupted()){
                System.out.println("释放资源");
                break;
            }
            System.out.println("Thread"+i);
        }
    }
}

5.线程组

package 多线程;
//批量管理
public class 线程组 {
    public static void main(String[] args) {
        MyRunnable r = new MyRunnable();
        //Thread t1 = new Thread(r,"线程1");
        //Thread t2 = new Thread(r,"线程2");

        //创建线程组
        ThreadGroup tg = new ThreadGroup("我们的线程组");
        Thread t1 = new Thread(tg,r);
        Thread t2 = new Thread(tg,r);
        System.out.println(t1.getThreadGroup().getName());
        System.out.println(t2.getThreadGroup().getName());

        tg.setDaemon(true);//设置守护线程
        tg.setMaxPriority(9);//设置最大有限级别

        /*ThreadGroup threadGroup1 = t1.getThreadGroup();
        ThreadGroup threadGroup2 = t2.getThreadGroup();//获取线程组
        System.out.println(threadGroup1.getName());//获取线程组名字
        System.out.println(threadGroup2.getName());//*/

        t1.start();
        t2.start();
    }


}

class MyRunnable implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName()+"  "+i);
        }
    }
}

6.设置线程优先级

package 多线程;

public class 设置线程优先级 {
    public static void main(String[] args) {
        Thread mainThread = Thread.currentThread();
        mainThread.setPriority(10);//设置为最大
        Thread1 thread1 = new Thread1();
        thread1.setPriority(1);//  置为最小
        System.out.println(mainThread.getPriority());
        System.out.println(thread1.getPriority());
        for (int i = 0; i < 100; i++) {
            System.out.println("mainThread"+i);
        }
        thread1.start();//启动那个线程
    }
}

class Thread1 extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("Thread1"+i);
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值