10.10 多线程进阶

线程控制

线程的停止

stop   即将被废除

自己的删除方法 循环。

Thread t1=new Thread(new Test_0014());
        t1.setName("实现类线程");
        t1.start();
        Thread.currentThread().setName("zhu");
        System.out.println("123456");

        Test_0014.setIsStop(false);  //  修改布尔类型表达式


}
class Test_0014 implements Runnable {
    static boolean isStop=true;
    @Override
    public void run() {

       while (true){  
           if (isStop) {
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yy,MM,dd,HH:mm:ss");
               String format = simpleDateFormat.format(new Date());
               System.out.println(Thread.currentThread().getName()+ format);
           }else {
               return;
           }
           }

    }

    public static boolean isIsStop() {
        return isStop;
    }

    public static void setIsStop(boolean isStop) {
        Test_0014.isStop = isStop;
    }
}

线程合并

join合并

public class Three_002 {
    public static void main(String[] args) {
      Thread thread= new Three_022();
      thread.setName("子线程");
        try {
            thread.join();  子线程合并到主线程中
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.start();
        Thread.currentThread().setName("main");
        for (int i = 6; i <11;i++ ) {
            System.out.println(Thread.currentThread().getName() + i);
}

//        Test_0014 a=new Test_0014();
//        a.run();
//      Test_0014.setIsStop(true);


    }
}
class Three_022 extends Thread{
    @Override
    public void run() {
        for (int i = 1; i < 5;i++) {
            System.out.println(this.getName()+i);
}

让位 yieid()  资源紧张的时候+

特点

  • 是个静态方法 ,写在谁里边谁让位
  • 同优先级让位不同优先级不让位
  • 使当前线程从执行状态(运行状态)变为可执行态(就绪状态)。cpu会从众多的可执行态里选择,也就是说,当前也就是刚刚的那个线程还是有可能会被再次执行到的,并不是说一定会执行其他线程而该线程在下次中不会执行到了。

 线程的同步

异步操作 

火车票窗口联系 

public class Tickets_01 implements Runnable {
    static int tickets=100;
    @Override
    public void run() {
        while (true) {
            if (tickets>0){
                System.out.println(Thread.currentThread().getName()+"售票窗口"+"tick名字"+tickets--);
            }else {
                break;
            }
        }


    }
}
ackage day1010;

/**
 * @author Wxx
 */
public class Tickets {
    public static void main(String[] args) {
       Tickets_01 t=new Tickets_01();
       Thread t1=new Thread(t);
       t1.setName("1号窗口");
       Thread t2=new Thread(t);
       t2.setName("2号窗口");
       Thread t3=new Thread(t);
       t3.setName("3号窗口");
        try {
            t1.join();
            t2.join();
            t3.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.start();
       t2.start();
       t3.start();

    }
}

同步 

  三种方法:synchronized      synchronized (){}   Lock

  • synchronized   是修饰符, 是锁的意思 让方法变成同步方法
  • 当执行加锁的成员方法时,该对象中其他加锁的成员方法,也被锁住
  • 当执行加锁的静态方法,该类中其他加锁的静态方法,也被锁住。
public synchronized void add(){/ 加在返回值前边
}

public class Tickets {
    public static void main(String[] args) {
       Tickets_01 t=new Tickets_01();
       Thread t1=new Thread(t);
       t1.setName("1号窗口");
       Thread t2=new Thread(t);
       t2.setName("2号窗口");
       Thread t3=new Thread(t);
       t3.setName("3号窗口");
        t1.start();
       t2.start();
       t3.start();

    }
}
public class Tickets_01 implements Runnable {
    static int tickets=100;
    @Override
    public synchronized void run() {  //  加锁后就变成同步方法了
        while (true) {
            if (tickets>0){
                System.out.println(Thread.currentThread().getName()+"售票窗口"+"tick名字"+tickets--);
            }else {
                break;
            }
        }


    }
}

2.synchronized(){}  代码块 想给谁加锁把谁写到大括号中

注意点

  1. synchronized(this)写在成员方法中,括号里写this
  2. 1.synchronized(类名.class)写在静态方法中,括号里写(类名.class)
  3.   2.当执行synchronized(this)代码块时,该对象其他加锁的成员,方法和加锁的代码块都被锁住
  4. 当执行synchronized(类名.class)代码块时,该类其他加锁的,静态方法和加锁的代码块都被锁住

3.lock

public class Lock_01 {
    Lock lock=new ReentrantLock();  创建lock 锁对象
    int a;
    public void add(){
        System.out.println("==========");

          lock.lock();  开启锁
          try {
              a++;
              System.out.println(a);    加锁的内容
          }finally {
              lock.unlock(); 关闭锁  释放锁
          }


            System.out.println("==========");


}
}

优先使用顺序:
 优先使用Lock显示锁 同步代码块(已经进入了方法体,分配了相应资源)à同步方法
(在方法体之外)

不会释放锁的操作

应尽量避免使用suspend() 暂停和resume(继续 来控制线程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值