1.AtomicInteger
类,getAndDecrement()
是AtomicInteger
类中的一个方法,它可以原子性地将当前值减1并返回减1前的值。这个方法的作用相当于先获取当前值,然后再将当前值减1。这个方法的返回值是减1前的值。在多线程编程中,getAndDecrement()
方法可以用来实现线程安全的自减操作。由于它是原子性的,因此多个线程可以同时调用这个方法而不会出现竞争条件。? ? ? ??
private AtomicInteger ticket = new AtomicInteger(100);
public void run() {
while (true) {
int remainingTickets = ticket.getAndDecrement();
if (remainingTickets > 0) {
System.out.println(Thread.currentThread().getName() + " is selling ticket " + remainingTickets);
} else {
break;
}
}
}
2.volatile
关键字来实现
class Ticket implements Runnable {
private volatile int ticket = 100;
public void run() {
while (ticket > 0) {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + " is selling ticket " + ticket--);
}
}
}
}
3.synchronized代码块实现
class Ticket implements Runnable {
private int ticket = 100;
public void run() {
while (true) {
synchronized(this) {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + " is selling ticket " + ticket--);
}
}
}
}
}
4.ReentrantLock
实现,与synchronized
相同的功能,但它具有更多的灵活性。例如,它允许线程以非阻塞方式获取锁,并且可以响应中断。?
class Ticket implements Runnable {
private int ticket = 100;
private final Lock lock = new ReentrantLock();
public void run() {
while (true) {
lock.lock();
try {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + " is selling ticket " + ticket--);
}
} finally {
lock.unlock();
}
}
}
}
5.ThreadLocal可以将数据封装在各自的线程中,每个ThreadLocal都可以存储一个线程级别的变量,且它本身可以被多个线程共享使用,并且能够实现线程安全。
public class TicketSeller {
private static ThreadLocal<Integer> tickets = new ThreadLocal<Integer>() {
@Override
protected Integer initialValue() {
return 100;
}
};
public void sell() {
Integer ticket = tickets.get();
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + " sells ticket " + ticket);
ticket--;
tickets.set(ticket);
}
}
}