Java线程同步代码编写

本文对比了Java中synchronized和ReentrantLock的并发控制机制,介绍了它们在锁的公平性、对象锁定、应用场景及性能差异。重点讨论了synchronized的四种用法和ReentrantLock的公平/非公平特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java中用于并发控制的方式比较多。主要有Lock和synchronized等。使用时需要能理解原理才行。
synchronized的锁是非公平锁,而ReentrantLock(JDK1.5)通过构造函数可以指定锁为公平或非公平。

synchronized

synchronized可以修饰非静态方法,静态方法,代码块等,其实本质还是synchronized锁的对象,总体说来,能成为锁的就是2个,一个是实例,一个是Class,其中修饰静态方法时锁定的时Class,修饰非静态方法时锁定的是实例,修饰代码块时可以选择锁定Class或者对象,如果选择锁定Object.class,则意味虚拟机内的全局锁。

修饰非静态方法

下面的2个实例中的方法,无法形成互斥,会并发执行。

public class S {

    public synchronized void f() {
        System.out.println(System.currentTimeMillis());
        AsyncUtil.safeSleep(3000);
        System.out.println(System.currentTimeMillis());
    }

}

public class SynchronizedTest {

    public static void main(String[] args) {
        S s1 = new S();
        S s2 = new S();

        new Thread(s1::f).start();
        new Thread(s2::f).start();

    }
}

修饰静态方法

被修饰的静态方法,在虚拟机中能达到全局互斥的效果。

public class S2 {

    public static synchronized void f() {
        System.out.println(System.currentTimeMillis());
        AsyncUtil.safeSleep(3000);
        System.out.println(System.currentTimeMillis());
    }
}

public class SynchronizedTest {

    public static void main(String[] args) {
        S2 s1 = new S2();
        S2 s2 = new S2();
        new Thread(S2::f).start();
        new Thread(S2::f).start();
    }
}

修饰代码块(锁this对象)

锁this对象还是针对某个实例的,所以这种写法的效果和修饰非静态方法是一样的。以下方法无法形成互斥,会并发执行。

public class S3 {

    public void f() {
        synchronized (this) {
            System.out.println(System.currentTimeMillis());
            AsyncUtil.safeSleep(3000);
            System.out.println(System.currentTimeMillis());
        }
    }
}

public class SynchronizedTest {

    public static void main(String[] args) {
        S3 s1 = new S3();
        S3 s2 = new S3();
        new Thread(s1::f).start();
        new Thread(s2::f).start();
    }
}

修饰代码块(锁Class)

基本上在虚拟机内能形成全局的互斥效果。

public class S4 {

    public void f() {
        synchronized (S4.class) {
            System.out.println(System.currentTimeMillis());
            AsyncUtil.safeSleep(3000);
            System.out.println(System.currentTimeMillis());
        }
    }
}

public class SynchronizedTest {

    public static void main(String[] args) {
        S4 s1 = new S4();
        S4 s2 = new S4();
        new Thread(s1::f).start();
        new Thread(s2::f).start();
    }
}

synchronized在Spring中的应用

public abstract class AbstractApplicationContext extends DefaultResourceLoader
		implements ConfigurableApplicationContext {

    private final Object startupShutdownMonitor = new Object();

	@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				initMessageSource();

				// Initialize event multicaster for this context.
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				onRefresh();

				// Check for listener beans and register them.
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				finishRefresh();
			}

			catch (BeansException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}

				// Destroy already created singletons to avoid dangling resources.
				destroyBeans();

				// Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}

			finally {
				// Reset common introspection caches in Spring's core, since we
				// might not ever need metadata for singleton beans anymore...
				resetCommonCaches();
			}
		}
	}


}

ReentrentLock

ReentrantLock可以指定为公平锁和非公平锁,公平锁下的线程都会先入队列进行休眠,然后由OS调度队列第一个线程,非公平锁下的线程会先尝试获取锁,没有抢到锁的线程才会到队列休眠,等待OS调度唤醒。非公平锁下的资源利用率比公平锁下的要高,RentrantLock默认是非公平锁,可以手动输入参数选择公平或非公平。

@Slf4j
public class ReentrantLockTest {

    private final ReentrantLock lock = new ReentrantLock(false);
    private final ReentrantLock lock2 = new ReentrantLock();
    private final ReentrantLock lock3 = new ReentrantLock(true);

    public void fun() {
        lock.lock();
        try {
            log.info("do something");
            int i = 1 / 0;
        } finally {
            log.info("unlock");
            lock.unlock();
        }
    }

    public void fun2() throws InterruptedException {
        if (!lock.tryLock(3, TimeUnit.SECONDS)) {
            log.warn("get lock failed");
            return;
        }
        try {
            log.info("do something");
            int i = 1 / 0;
        } finally {
            log.info("unlock");
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReentrantLockTest test = new ReentrantLockTest();
        test.fun2();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值