12_happenBefore

HappenBefore

指令重排:执行代码的顺序可能与编写的代码顺序不一致,即虚拟机优化代码,则为指令重排,目的是为了优化程序性能

happen-before:编译器或运行时环境为了优化程序性能而采取的对指令重新排序执行的一种手段

例子:
package com.tsymq.thread.advanced;

public class HappenBefore {
    private static int a = 0;

    private static boolean flag = false;

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            a = 0;
            flag = false;
            // 线程1 读取数据
            Thread t1 = new Thread( () -> {
                a = 1;
                flag = true;
            });

            // 线程2 更改数据
            Thread t2 = new Thread( () -> {
                if(flag){
                    a *= 1;
                }
                if(a == 0){
                    System.out.println("happen before: a = " + a);
                }
            });

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

            try {
                t1.join();
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

可能出现控制台输出 happen before: a = 1 的情况

volatile

  • 用于保证数据的同步,即可见性 (轻量级synchronized)

  • 可以避免指令重排

  • 会破坏原子性

public class VolatileTest {
    private static int num = 0;
    public static void main(String[] args) {
        new Thread( () -> {
            while(num == 0){
            }
        }).start();
        // 1秒后改变num的值
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        num = 1;
    }
}

代码运行,程序不会停止,因为while(num == 0)的死循环导致机器没有时间去将num的值同步更新

public class VolatileTest {
    private volatile static int num = 0;
    public static void main(String[] args) {
        new Thread( () -> {
            while(num == 0){
            }
        }).start();
        // 1秒后改变num的值
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        num = 1;
    }
}

在声明num变量时添加了volatile关键字,程序1秒钟后会停止运行

单例模式

套路:

  1. 构造器私有化,外部无法用new构造
  2. 提供私有的静态属性,存储对象的地址
  3. 提供公共的静态方法,获取属性

在并发控制下使用double-checking、volatile、synchronized实现单例模式

package com.tsymq.thread.advanced;
public class DoubleCheckedLocking {
    private volatile static DoubleCheckedLocking instance;   // 懒汉式
    // 没有volatile,其他线程可能会拿到一个未初始化的空对象
    
    private DoubleCheckedLocking(){
    }
    public static DoubleCheckedLocking getInstance(){
        // 再次检测
        if (null != instance){
            return instance;
        }
        synchronized (DoubleCheckedLocking.class){
            if(null == instance){
                instance = new DoubleCheckedLocking();
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        Thread t = new Thread( ()-> {
            System.out.println("1" + DoubleCheckedLocking.getInstance());
        });
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("2" + DoubleCheckedLocking.getInstance());
    }
}
create table if not exists tb_log_gps ( id bigint not null, device_id varchar not null, platform_id varchar, location varchar, happen_time varchar, create_time TIMESTAMP NOT NULL DEFAULT now() ); CREATE INDEX idx_tb_log_gps_id ON tb_log_gps(id); CREATE INDEX idx_tb_log_gps_happen_time ON tb_log_gps(happen_time); CREATE OR REPLACE FUNCTION insert_log_gps_partition_func() RETURNS TRIGGER AS $$ DECLARE date_text TEXT; insert_statement TEXT; date_part DATE := '2000-01-01'; date_next DATE := '2000-01-01'; BEGIN SELECT SUBSTRING(NEW.happen_time,1,10) INTO date_text; insert_statement := 'INSERT INTO tb_log_gps_' || date_text ||' VALUES ($1.*);'; EXECUTE insert_statement USING NEW; RETURN NULL; EXCEPTION WHEN UNDEFINED_TABLE THEN date_part := ('''' || date_part('year'::text, to_date(NEW.happen_time,'yyyy-MM-dd hh24:mi:ss')) || '-' || date_part('month'::text, to_date(NEW.happen_time,'yyyy-MM-dd hh24:mi:ss')) || '-' || date_part('day'::text, to_date(NEW.happen_time,'yyyy-MM-dd hh24:mi:ss')))::DATE; date_next := date_part + '1 day'::interval; EXECUTE 'CREATE TABLE IF NOT EXISTS tb_log_gps_' || date_text || '(CHECK(' || 'happen_time' || '>= ''' || date_part::text || ''' AND ' || 'happen_time' || '< ''' || date_next::text || ''')) INHERITS (tb_log_gps);'; EXECUTE 'create index idx_tb_log_gps_' || date_text || '_happen_time on tb_log_gps_' || date_text || '(happen_time);'; EXECUTE 'create index idx_tb_log_gps_' || date_text || '_id on tb_log_gps_' || date_text || '(id);'; EXECUTE insert_statement USING NEW; RETURN NULL; END; $$ LANGUAGE plpgsql VOLATILE; DROP TRIGGER IF EXISTS insert_tb_log_gps_partition_trigger ON tb_log_gps; CREATE TRIGGER insert_tb_log_gps_partition_trigger BEFORE INSERT ON tb_log_gps FOR EACH ROW EXECUTE PROCEDURE insert_log_gps_partition_func(); 插入数据报错,这个分表触发器哪里错了
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值