ThreadLocal ——为每个使用公共变量的线程提供独立的变量副本

ThreadLocal不同于synchronized,它为每个线程提供独立的变量副本,避免并发访问冲突。线程中ThreadLocal对象是弱引用,防止内存泄漏,但需注意及时调用remove以清理value。

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

 synchronized同步机制通过对象的锁机制保证同一时间只有一个线程访问多个线程共享的变量,要求程序慎密地分析什么时候对变量进行读写,什么时候需要锁定某个对象,什么时候释放对象锁等繁杂的问题。而ThreadLocal则用另一种思路解决多线程的并发访问,它通过为每个线程提供一个独立的变量副本解决了变量并发访问的冲突问题。

public class ThreadLocalStu{

    static ThreadLocal<Student> tl = new ThreadLocal<>();

    public static void main(){

        new Thread(()->{
            try{
                TimeUnit.SECONDS.sleep(timeout:2);
            }catch(InterruptedException e) {
                e.printStackTrace();
            }
            System.out.print(tl.get());
        }).start();
    
        new Thread(()->{
            try{
                TimeUnit.SECONDS.sleep(timeout:1);
            }catch(InterruptedException e) {
                e.printStackTrance();
            }
            tl.set(new Student())  //tl是map的key,new Student()是map的value
        }).start();
    }

    static class Student{
        String name = "Meimei"
    }

}

这两个线程中&

uni-app 中,可以通过 Native.js 实现安卓本地通知,具体步骤如下: 1.App.vue 文件中,监听应用启动事件,调用 Native 模块中的 startForeground 方法开启前台服务: ```javascript export default { onLaunch() { const nativeModule = uni.requireNativePlugin("NativeModule"); nativeModule.startForeground(); } } ``` 2.Native 模块中,实现 startForeground 方法,并在该方法中创建通知渠道和通知: ```javascript const Context = require("@android/context"); const Notification = require("@android/app/Notification"); const NotificationManagerCompat = require("@android/app/NotificationManagerCompat"); const NotificationChannel = require("@android/app/NotificationChannel"); function startForeground() { const service = getContext().getService(); const channel = new NotificationChannel("channel_id", "channel_name", NotificationManagerCompat.IMPORTANCE_HIGH); const notificationManager = NotificationManagerCompat.from(Context); const notification = new Notification.Builder(Context, "channel_id") .setSmallIcon(android.R.drawable.ic_dialog_info) .setContentTitle("Foreground Service") .setContentText("Service is running in foreground") .build(); service.startForeground(1, notification); } ``` 3.Native 模块中,实现 showNotification 方法,用于创建本地通知: ```javascript function showNotification(title, content) { const notificationManager = NotificationManagerCompat.from(Context); const notification = new Notification.Builder(Context, "channel_id") .setSmallIcon(android.R.drawable.ic_dialog_info) .setContentTitle(title) .setContentText(content) .build(); notificationManager.notify(1, notification); } ``` 其中,第一个参数为通知的 ID,第二个参数为 Notification 对象。 4.App.vue 文件中,调用 Native 模块中的 showNotification 方法创建本地通知: ```javascript export default { methods: { showLocalNotification() { const nativeModule = uni.requireNativePlugin("NativeModule"); nativeModule.showNotification("Local Notification", "This is a local notification"); } } } ``` 这样就可以通过 Native.js 实现安卓本地通知了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值