微信tocken後臺後臺儲存方法

明陽發表於2018-10-18

直接上程式碼 理解單例模式就可以

  public class MyTocken {
    private static MyTocken instance;
    private String Tocken;
    private Long second;
    public MyTocken(String tocken, Long second) {
        System.out.println("MyTocken has loaded");
        Tocken = tocken;
        this.second = second;
    }

    public static MyTocken getInstance(){
        if(instance==null){
            synchronized (MyTocken.class){
                if(instance==null){
                    instance=new MyTocken("",System.currentTimeMillis());
                }
            }
        }
        return instance;
    }

    public String getTocken() {
        return Tocken;
    }

    public void setTocken(String tocken) {
        Tocken = tocken;
    }

    public Long getSecond() {
        return second;
    }

    public void setSecond(Long second) {
        this.second = second;
    }

    @Override
    public String toString() {
        return "MyTocken{" +
                "Tocken='" + Tocken + '\'' +
                ", second=" + second +
                '}';
    }

    public static void main(String[] args) throws InterruptedException {

        getMyTocken();

    }

    public static String getMyTocken(){
        MyTocken myTocken=  MyTocken.getInstance();
        long startTime = System.currentTimeMillis();
        double timeDouble= Double.parseDouble(Long.toString(System.currentTimeMillis()-myTocken.getSecond()));
        if(timeDouble>7000){
            System.out.println(myTocken.getSecond()+"我是大於7000秒獲取來的");
            //這裡 寫你記得程式碼
            myTocken.setSecond(startTime);
        }else{
            myTocken.setSecond(startTime);
            //這裡 寫你記得程式碼
            System.out.println(myTocken.getSecond()+"我是小於7000秒獲取來的");
        }

        return ""+timeDouble;
    }複製程式碼

}

相關文章