java基础

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("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值