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 + "\n");
}
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

最新文章

  1. Java 消息摘要 散列 MD5 SHA
  2. JVM之SerialOld收集器
  3. 数独挑战(codevs 2924)
  4. js方式找出数组中重复数最多的那个数,并返回该数以及重复次数
  5. java list中的对象去重原理
  6. [ionic开源项目教程] - 第8讲 根据菜单分类加载数据(重要)
  7. jquery实现简单的ajax
  8. Struts 2 OGNL
  9. C#关于HttpClient的应用(二):极光推送IM集成
  10. 判断手机电脑微信 js
  11. Android 自定义 permission
  12. c++中类对象的内存对齐
  13. 【代码笔记】Web-CSS-CSS Text(文本)
  14. CentOS 6 update curl
  15. IntelliJ IDEA 注册码 (秘钥)
  16. Dos命令大全完整版
  17. yum provides &quot;*/nmcli&quot; and apt-get
  18. 校园跳蚤市场-Sprint计划(第二阶段)
  19. 【逆向知识】PE ASLR
  20. 在程序开发中怎样写SQL语句可以提高数据库的性能

热门文章

  1. pgsql 数据类型
  2. 【题解】At2370 Piling Up
  3. SAP 4代增强
  4. kinect/xiton 的环境搭建 + rgb图像和深度图的标定
  5. Linux下监视GPU、CPU的使用情况
  6. Qt &amp; MySQL
  7. IOS UITableView reload 刷新某一个cell 或 section
  8. MVC+Ext.net零基础学习记录(四)
  9. Ubuntu apt-get update 失败【转】
  10. Starting MySQL.. ERROR! The server quit without updating PID file