1.枚举
2.内置三种注解
- @Override
- @Deprecated
- @SuppressWarnings
3.元注解:用于注解其它注解
4.注解处理器:通过反射
5.创建线程的两种方式
- 实现Runnable接口:推荐
- 继承Thread类
package com.chengjie; class ThreadImpl1 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ThreadImpl1.fun() is called!");
}
}
} class ThreadImpl2 extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ThreadImpl2.fun() is called!");
}
}
} public class TestThread {
public static void main(String[] args) {
Thread t1 = new Thread(new ThreadImpl1());
Thread t2 = new ThreadImpl2();
t1.start();
t2.start();
}
}
6.后台(daemon)线程
7.共享资源
- 访问资源方法上添加synchronized关键字
- 同步控制块:synchronized(this)
- 显式使用lock:java.util.concurrent
- volatile:发生在变量上的写操作对所有的读操作可见