单例双式以及端点续传

本文详细解析了单例模式的两种实现方式——饿汉式与懒汉式,并探讨了它们在线程安全方面的考量。此外,还介绍了如何在Android应用中实现文件的断点续传功能,包括通过HTTP请求获取文件总长度和断点位置,以及使用随机流进行文件下载。

单例模式

总体思路:先创建一个私有的private static 的对象,再创建一个私有的构造,防止外部调用修改;最后创建一个对外开放的静态的对象实例,返回值为return 对象;
饿汗式是先new出对象,等待调用;懒汉式是先创建对象为null,之后等待调用,只有当被调用时才会new出对象

饿汗式
 //饿汗式
    private static Utils utils = new Utils();
    
    //私有构造
    private Utils(){
        
    }
    
    //提供对外公开的方法
    public static Utils getInstance(){
        return utils;
    }
懒汉式

需要上锁,双重校验锁,否则会造成线程安全问题

 //懒汉式
    private static Utils utils = null;

    //私有构造
    private Utils(){

    }

    //提供对外公开的方法
    private static Utils getInstance(){
        if (utils == null) {
            synchronized (Object.class){
                if (utils == null) {
                    utils = new Utils();
                }
            }
        }
        return utils;
    }

断点续传

2次请求,第一次获取文件总长度,第二次获取从SD卡中获取的文件长度并继续用随机流下载

package com.example.work;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MyThread extends Thread {

    private static final String TAG = "---";

    private String path;
    private String str_url;
    private Handler handler;

    public MyThread(String path, String str_url, Handler handler) {
        this.path = path;
        this.str_url = str_url;
        this.handler = handler;
    }
    private long end; //总长度
    private long start; //SD卡文件的长度
    @Override
    public void run() {

        try {
            URL url = new URL(str_url);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            int responseCode = connection.getResponseCode();
            Log.i(TAG, "code: "+responseCode);

            if (responseCode == 200) {
                end = connection.getContentLength();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        File file = new File(path);
        if(file.exists()){
            start = file.length();
        }else{
            start = 0;
        }


        try {
            URL url = new URL(str_url);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestProperty("Range","bytes="+start+"-"+end);

            connection.connect();
            int responseCode = connection.getResponseCode();
            Log.i(TAG, "code: "+responseCode);
            Log.i(TAG, "run: "+start+" - "+end);
            if (responseCode == 206) {
                int len = 0;
                byte[] b = new byte[1024];
                InputStream is = connection.getInputStream();

                RandomAccessFile randomAccessFile = new RandomAccessFile(path,"rw");
                randomAccessFile.seek(start);

                long count = start;

                while((len = is.read(b)) != -1){
                    randomAccessFile.write(b, 0, len);

                    count += len;
                    int values = (int) ((float)count/end*100);
                    Message msg = Message.obtain();
                    msg.what = 0;
                    msg.obj = values;
                    Log.i(TAG, "values: "+values);
                    handler.sendMessage(msg);
                }
                is.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

综合技能

package com.example.work;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private MyAdapter adapter;

    private String path2 = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";
    public static List<String> isLoad = new ArrayList<>();

    private List<JavaBean.DataBean> dataList = new ArrayList<>();
    public static List<String> name = new ArrayList<>();
    private int index = 0;

    private ProgressDialog progressDialog;
    private File directory;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                isLoad.set(index,"已下载");
                adapter.notifyDataSetChanged();
            } else if (msg.what == 1) {
                progressDialog.setProgress((Integer) msg.obj);
                progressDialog.dismiss();
                if((Integer) msg.obj == 100){
                    Toast.makeText(MainActivity.this, "下载成功!", Toast.LENGTH_SHORT).show();
                }
            } else if (msg.what == 2) {
                Toast.makeText(MainActivity.this, "已经下载过了", Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();
            }
        }
    };

    private long start = 0;
    private long end = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1000);

        initView();
    }

    private void initView() {
        listView = (ListView) findViewById(R.id.list_view);

        adapter = new MyAdapter(dataList,MainActivity.this);
        listView.setAdapter(adapter);

        new MyAsyncTask(dataList,adapter).execute(path2);

        listListener();
    }

    private void listListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

                MyDialog(position);
            }
        });

    }

    private void MyDialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

        builder.setMessage("是否下载?");
        builder.setTitle("提示信息");

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setMessage("下载中。。。");
                index = position;
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        try {
                            URL url = new URL(dataList.get(position).getPic());
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                            if (connection.getResponseCode() == 200) {
                                end = connection.getContentLength();
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        directory = Environment.getExternalStorageDirectory();
                        File file = new File(directory, name.get(position));
                        if (file.exists()) {
                            start = file.length();
                        }else{
                            start = 0;
                        }
                        if(start == end){
                            handler.sendEmptyMessage(2);
                            return;
                        }

                        try {
                            URL url = new URL(dataList.get(position).getPic());
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                            connection.setRequestProperty("Range","bytes="+start+"-"+end);
                            connection.connect();
                            if (connection.getResponseCode() == 206) {
                                int len = 0;
                                byte[] b = new byte[1024];
                                InputStream is = connection.getInputStream();
                                FileOutputStream fos = new FileOutputStream(new File(directory,name.get(position)));

                                long count = start;

                                while ((len = is.read(b)) != -1) {
//                                    Thread.sleep(1000);

                                    fos.write(b, 0, len);
                                    count += len;

                                    int values = (int) ((float)count/end*100);

                                    Message obtain = Message.obtain();
                                    obtain.what = 1;
                                    obtain.obj = values;
                                    handler.sendMessage(obtain);
                                }

                                handler.sendEmptyMessage(0);

                                connection.disconnect();
                                is.close();
                                fos.close();
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }).start();

                progressDialog.show();
            }

        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }


}

【直流微电网】径向直流微电网的状态空间建模与线性化:一种耦合DC-DC变换器状态空间平均模型的方法 (Matlab代码实现)内容概要:本文介绍了径向直流微电网的状态空间建模与线性化方法,重点提出了一种基于耦合DC-DC变换器状态空间平均模型的建模策略。该方法通过对系统中多个相互耦合的DC-DC变换器进行统一建模,构建出整个微电网的集中状态空间模型,并在此基础上实施线性化处理,便于后的小信号分析与稳定性研究。文中详细阐述了建模过程中的关键步骤,包括电路拓扑分析、状态变量选取、平均化处理以及雅可比矩阵的推导,最终通过Matlab代码实现模型仿真验证,展示了该方法在动态响应分析和控制器设计中的有效性。; 适合人群:具备电力电子、自动控制理论基础,熟悉Matlab/Simulink仿真工具,从事微电网、新能源系统建模与控制研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握直流微电网中多变换器系统的统一建模方法;②理解状态空间平均法在非线性电力电子系统中的应用;③实现系统线性化并用于稳定性分析与控制器设计;④通过Matlab代码复现和扩展模型,服务于科研仿真与教学实践。; 阅读建议:建议读者结合Matlab代码逐步理解建模流程,重点关注状态变量的选择与平均化处理的数学推导,同时可尝试修改系统参数或拓扑结构以加深对模型通用性和适应性的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值