1. 守护线程:
当所有非后台线程都终止后,则虚拟机退出,并且会立即终止这个线程
public class Test {
public static void main(String[] args) {
Thread t = new Thread(new Runnable(){
public void run() {
System.out.println("Daemon thread");
try {
TimeUnit.SECONDS.sleep(10);
System.out.println("守护线程执行完成");
} catch (InterruptedException e) {
}
}
});
t.setDaemon(true);
t.start();
System.out.println("main线程执行完成");
}
}
2. 实现优雅退出
public static void main(String[] args) {
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
}
System.out.println("将积压的任务处理完成");
}
}));
System.out.println("main线程执行完成");
System.exit(0);
}
3. maven打包时将依赖的jar包添加到lib目录
mvn dependency:copy-dependencies -DoutputDirectory=lib
4. 将html中的大于号和小于号转义
String str = "<h1>hello</h1>";
str = str.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
System.out.println(str);
5. JSON (javascript object notation js对象声名)
是一种轻量级的数据交换格式。它基于 ECMAScript (w3c制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
轻量级:是相对于xml而言,xml大,解析速度慢;
数据交换:将数据转换成一种与平台无关的数据格式,然后转交给接收方来处理
表示一个对象:
{属性名:属性值,属性名:属性值}
注意:属性名必须用双引号,属性值必须是number,string,true/fasle,null,object,属性值如果是string,必须用双引号
exp: var obj = {“name”:“Sally”,“age”:22}
表示对象组成的数组
[{“name”:“sally”,“age”:22},{“name”:“dabin”,“age”:22}]
6. 泛型
背景:提供者不满足使用者需求
public class Test<KEY,VALUE> {
private KEY project;
private VALUE score;
//省略get/set
解决用户自定义类型以满足不同需求
public class MainTest {
public static void main(String[] args) {
Test<String, Integer> t1 = new Test<String, Integer>();
t1.setProject("yuwen");
t1.setScore(78);
Test<Integer, String> t2 = new Test<Integer, String>();
t2.setProject(1);
t2.setScore("一");
}
}
7.是否为调试模式
boolean isDebug = ManagementFactory.getRuntimeMXBean().getInputArguments().toString().contains("jdwp");
8 CountDownLatch用法
public class Test {
public static void main(String[] args) throws Exception {
CountDownLatch latch = new CountDownLatch(1);
new Thread("count-down-thread") {
public void run() {
System.out.println(Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
System.out.println(e);
}
System.out.println(Thread.currentThread().getName());
latch.countDown();
}
}.start();
latch.await();
System.out.println(Thread.currentThread().getName());
}
}
- 使用java1.8
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerArgument>-parameters</compilerArgument>
<source>1.8</source>
<target>1.8</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<compilerArgs>
<arg>-Werror</arg>
<arg>-Xlint:all</arg>
<!--not care for jdk8/jdk7 compatible problem -->
<arg>-Xlint:-classfile</arg>
<!--not care for annotations not processed -->
<arg>-Xlint:-processing</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>