线程中的一些常用方法 和线程安全问题

1.Therad类中的一些常用的一些方法

1.1线程休眠方法 static void sleep

public class MyThread extends Thread{

    @Override
    public void run() {
        for (int i = 0; i <20 ; i++) {
            try {
                //秒杀---
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName()+"~~~~~~~~~~~~~~~"+i);
        }
    }
}
1.2yield 当前线程让出cup 参与下次竞争
public class MyThread extends Thread{

    @Override
    public void run() {
        for (int i = 0; i <20 ; i++) {
            System.out.println(Thread.currentThread().getName()+"~~~~~~~~~~~~~~~"+i);
            Thread.yield();//当前线程让出cpu参与下次的竞争。
            System.out.println("~~~~~~~~~~~~~~~~");
        }
    }
}
1.3join加入当前线程上

插入线程执行完毕后,当前线程才会执行


public class Test {
    public static void main(String[] args) throws InterruptedException {
        MyThread t1=new MyThread();
        t1.setName("线程A");
        MyThread2 t2=new MyThread2();
        t2.setName("线程B");


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

        t1.join(); //t1加入主线程上,主线程需要等t1执行完毕后才会执行. 如果主线程需要等带t1和t2线程的执行结果 做下一步操作时。
        for (int i = 0; i <20 ; i++) {
            Thread.sleep(10);
            System.out.println("main~~~~~~~~~~~~~~~~~~~~~"+i);
        }
    }
}
1.4 setDaemon()设置线程为守护线程

当线程执行完毕后守护线程也会终止

>

```java
public class Test {
    public static void main(String[] args) throws InterruptedException {
        MyThread t1=new MyThread();
        t1.setName("线程A");
        t1.setDaemon(true);//设置t1为守护线程
        t1.start();
        for (int i = 0; i <20 ; i++) {
            System.out.println("main~~~~~~~~~~~~~~~~~~~~~"+i);
        }
    }
}
```

## 3.线程

2.线程的安全问题

2.1 什么情况下才会出现线程安全问题

当多个线程操作同一个资源时,则出现线程安全问题

2.2 java如何解决线程安全问题

提供了两种方式,
第一种:使用synchronized自动锁
第二种:使用Lock手动锁.
使用synchronized关键字解决

它可以使用在方法上也可以使用在代码块中

<br class="Apple-interchange-newline"><div></div>

synchronized(共享锁对象){
同步代码块。

}

public class SellTicket implements Runnable {
    private int tick = 100;
    private static Object o = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (tick > 0) {
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    tick--;
                    System.out.println(Thread.currentThread().getName() + "卖了一张票;剩余:" + tick + "张");
                } else {
                    break;
                }
            }
        }
    }
}
使用Lock手动

Lock它是一个接口,它的实现类ReentrantLock


public class SellTicket implements Runnable {
    private int tick = 100;

    private Lock l = new ReentrantLock();

    //synchronized使用在方法那么它的共享锁为this
    @Override
    public void run() {
        while (true) {
            try {
                l.lock();//加锁
                if (tick > 0) {
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    tick--;
                    System.out.println(Thread.currentThread().getName() + "卖了一张票;剩余:" + tick + "张");
                } else {
                    break;
                }
            } finally {
                l.unlock();//解锁
            }

        }
    }


}

  • 23
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值