zoukankan      html  css  js  c++  java
  • 7.19实习培训日志- java进阶

    java进阶

    java集合

    Collection

    List

    ArrayList

    jdk1.2,异步处理,性能高,线程不安全

    Vector

    jdk1.0,同步处理,性能低,线程安全

    Set

    HashSet

    散列存放

    hashCode是为了提高在散列结构存储中查找的效率,在线性表中没有作用。

    若两个对象equals返回true,则hashCode有必要也返回相同的int数。
    若两个对象equals返回false,则hashCode不一定返回不同的int数,但为不相等的对象生成不同hashCode值可以提高哈希表的性能。
    若两个对象hashCode返回相同int数,则equals不一定返回true。
    若两个对象hashCode返回不同int数,则equals一定返回false。

    equals()对比是绝对可靠,hashCode不是绝对可靠

    equals()效率低,hashcode效率高

    先通过hashcode比较,相等时在比较equals,相对效率更高。

    TreeSet

    有序存放

    TreeSet类型是J2SE中唯一可实现自动排序的类型

    Map

    //推荐,尤其是容量大时
    System.out.println("通过Map.entrySet遍历key和value");
    for (Map.Entry<String, String> entry : map.entrySet()) {
        System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
    }
    

    HashMap

    HashTable

    Iterator

    List<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    list.add("C");
    list.add("D");
    Iterator<String> iterator = list.iterator();
    while (iterator.hasNext()){
        list.remove(2);
        System.out.println(iterator.next());
    }
    //ConcurrentModificationException
    
    
    List<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()){
            String str = iterator.next();
            if(str.equals("A"))
                iterator.remove();
            else
                System.out.println(str);
        }
    }
    B
    C
    D
    

    java文件

    文件基本操作

    File file = new File("hello.txt");
    if(file.exists()){
        //file.delete();
        File file1 = new File("hello1.txt");
        //重命名文件必须处于同一个分区内,不在同一个分区可以使用拷贝文件
        file.renameTo(file1);
        System.out.println(file.isDirectory());
    }else{
        try {
            file.createNewFile();
        } catch (IOException e) {
            System.out.println("文件创建失败");
        }
    }
    

    java IO

    使用字节流读取文件

    try {
        FileInputStream fis = new FileInputStream("hello.txt");
        byte[] buffer = new byte[20];
        fis.read(buffer);
        String str = new String(buffer);
        System.out.println(str);
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    使用字节流写入文件

    try {
        FileOutputStream fos = new FileOutputStream("test.txt");
        String str = "test";
        byte[] buffer = str.getBytes("UTF-8");
        fos.write(buffer);
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    使用缓冲字节流读写文件

    try {
        FileInputStream fis = new FileInputStream("test.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream("test1.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        byte[] buffer = new byte[1000];
        bis.read(buffer);
        bos.write(buffer);
    
        bos.close();
        fos.close();
        bis.close();
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    使用缓存字符流读取文件

    File file = new File("hello.txt");
    if(file.exists()){
        try {
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine())!=null){
                System.out.println(line);
            }
            br.close();
            isr.close();
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }else{
        System.out.println("文件不存在");
    }
    

    对于文本文件读写:字符流效率高于字节流

    FileReader与 FileWriter

    try {
        FileReader fr = new FileReader("test.txt");
        BufferedReader br = new BufferedReader(fr);
        FileWriter fw = new FileWriter("test2.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        String line = null;
        while ((line = br.readLine()) != null) {
            bw.write(line + "
    ");
        }
        bw.flush();
        bw.close();
        fw.close();
        br.close();
        fr.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    RandomAccessFile

    完成随机读取功能,可以读取指定位置的内容。

    服务器的日志文件往往达到400多M,简单的文件读取实在太慢,太占用机器资源。如果需要统计一些即时数据。比如刚才10分钟的来访客户,即时扫描大数据文件中的一部分显得非常之重要,用RandomAccessFile。

    Apache IO库

    File file = new File("test.txt");
    FileUtils.readFileToString(file,"UTF-8");
    

    java 线程

    线程的状态转换

    run()方法是普通的方法调用,并不是启用线程,而start方法是首先启动一个新的线程,然后运行run()方法里面的代码。

    Thread.currentThread().getName() //得到当前线程名字
    

    A.join():堵塞当前线程B,直到A执行完毕并死掉,再执行B。

    A.yield():让出位置,给B执行,B执行结束A再执行。与join相反。

    Yield不能保证使得当前正在运行的线程迅速转换到可运行的状态,不保证一定产生效果,仅能使一个线程从运行状态转到可运行状态,而不是等待或阻塞状态.

    setPriority():设置线程优先级

    优先级并不是很靠谱,因为Java线程是通过映射到系统的原生线程上来实现的,所以线程调度最终还是取决于操作系统。

    Java线程调度就是抢占式调度。

    stop()被弃用,本身不安全,会导致线程逻辑不完整。破坏了原子逻辑.用自定义的标志位停止线程。

    线程的停止

    class SafeStopThread extends Thread {
        //此变量必须加上volatile
        private volatile boolean stop = false;
        @Override
        public void run() {
            //判断线程体是否运行
            while (!stop) {
                // Do Something
            }
        }
        //线程终止
        public void terminate() {
            stop = true;
            System.out.println("Stop");
        }
    }
    

    继承Thread类创建线程

    extend Thread,并复写run()方法

    实现Runnable接口创建线程

    实现run()方法

    Callable FutureTask

    实现Callable接口通过FutureTask包装器来创建Thread线程

    Callable callable = new MyCallable();
            FutureTask task = new FutureTask(callable);
            Thread thread = new Thread(task);
            thread.start();
            try {
                System.out.println(task.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
    
    ...
    
    class MyCallable implements Callable<Integer> {
    
        @Override
        public Integer call() throws Exception {
            return 13;
        }
    }
    

    使用ExecutorService、Callable、Future实现有返回结果的线程

    ExecutorService executorService = new ThreadPoolExecutor(3,3,0,TimeUnit.DAYS,new LinkedBlockingQueue<Runnable>(2));
            Callable callable1 = new MyCallable();
            FutureTask task1 = new FutureTask(callable);
            List<Future> futures = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                Callable c = new MyCallable();
                Future f = executorService.submit(c);
                futures.add(f);
            }
            for (Future f : futures) {
                System.out.println(">>>" + f.get().toString());
            }
    

    同步和死锁

    synchronized

  • 相关阅读:
    thinkphp在模型中自动完成session赋值
    highcharts实例教程二:结合php与mysql生成饼图
    程序员应该经常看看的网站
    highcharts实例教程一:结合php与mysql生成折线图
    2015-2-10 ecshop
    一个简单的javascript获取URL参数的代码
    table 西边框样式
    PHP 获取当前日期及格式化
    mysql 获取当前日期及格式化
    mysql时间int日期转换
  • 原文地址:https://www.cnblogs.com/sufferingStriver/p/9403428.html
Copyright © 2011-2022 走看看