一、多線程
1、線程的創建、啟動、停止、休眠、等待
public class ThreadDemo extends Thread {
public void run(){
System.out.println("線程的執行");
}
public static void main(String[] args){
ThreadDemo thread = new ThreadDemo();
thread.start(); // 啟動線程
}
}
2、線程池的使用
public class ThreadPoolDemo {
public static void main(String[] args){
ExecutorService executorService = Executors.newFixedThreadPool(5); // 創建固定大小的線程池
for(int i=0; i<10; i++){
executorService.execute(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName() + "正在執行任務");
}
});
}
executorService.shutdown(); // 關閉線程池
}
}
3、線程間的通信
public class ThreadCommunicationDemo {
public static void main(String[] args) {
final Business business = new Business();
new Thread(new Runnable() {
public void run() {
for(int i=0; i<50; i++){
business.sub();
}
}
}).start();
for(int i=0; i<50; i++){
business.main();
}
}
static class Business{
private boolean flag = true;
public synchronized void main(){
if(flag){
try {
this.wait(); // 如果flag為true,則等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(int i=0; i<5; i++){
System.out.println(Thread.currentThread().getName() + "運行第" + i + "次");
}
flag = true; // 設置flag為true
this.notify(); // 通知
}
public synchronized void sub(){
if(!flag){
try {
this.wait(); // 如果flag為false,則等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(int i=0; i<10; i++){
System.out.println(Thread.currentThread().getName() + "運行第" + i + "次");
}
flag = false; // 設置flag為false
this.notify(); // 通知
}
}
}
二、異常處理
1、try-catch-finally語句塊
public class TryCatchFinallyDemo {
public static void main(String[] args){
try {
int i = 1 / 0;
} catch (Exception e) {
e.printStackTrace();
} finally{
System.out.println("finally語句塊");
}
}
}
2、自定義異常類
public class CustomExceptionDemo {
public static void main(String[] args) throws CustomException{
int age = 10;
if(age < 18){
throw new CustomException("未滿18周歲,無法註冊");
} else {
System.out.println("註冊成功");
}
}
}
class CustomException extends Exception{
public CustomException(String message){
super(message);
}
}
3、finally語句塊中的return語句
public class FinallyReturnDemo {
public static void main(String[] args) {
System.out.println(getNum());
}
public static int getNum(){
try {
return 1;
} finally{
return 2;
}
}
}
三、泛型
1、泛型類、泛型方法
public class GenericDemo {
private T t;
public void setT(T t) {
this.t = t;
}
public T getT() {
return t;
}
public void printMsg(E e){
System.out.println(e.getClass());
}
public static void main(String[] args){
GenericDemo genericDemo = new GenericDemo();
genericDemo.setT("JAVA高級編程開發技巧");
System.out.println(genericDemo.getT());
genericDemo.printMsg("泛型方法"); // 輸出java.lang.String
GenericDemo genericDemo1 = new GenericDemo();
genericDemo1.setT(2019);
System.out.println(genericDemo1.getT());
genericDemo1.printMsg(123); // 輸出java.lang.Integer
}
}
2、泛型介面
public interface GenericInterface {
public void setT(T t);
public T getT();
public void printMsg(String msg);
}
class GenericImpl implements GenericInterface{
private T t;
public void setT(T t) {
this.t = t;
}
public T getT() {
return t;
}
public void printMsg(String msg) {
System.out.println(msg + ":" + t);
}
public static void main(String[] args){
GenericInterface genericInterface = new GenericImpl();
genericInterface.setT("泛型介面");
System.out.println(genericInterface.getT());
genericInterface.printMsg("JAVA高級編程開發技巧");
}
}
四、註解
1、自定義註解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
public String value() default "自定義註解";
}
@MyAnnotation
public class AnnotationDemo {
public static void main(String[] args){
Annotation[] annotations = AnnotationDemo.class.getAnnotations();
for(Annotation annotation : annotations){
if(annotation instanceof MyAnnotation){
MyAnnotation myAnnotation = (MyAnnotation) annotation;
System.out.println(myAnnotation.value()); // 輸出自定義註解
}
}
}
}
2、元註解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface MyAnnotation {
public String value() default "元註解";
}
@MyAnnotation
public class AnnotationDemo {
public static void main(String[] args){
Annotation[] annotations = AnnotationDemo.class.getAnnotations();
for(Annotation annotation : annotations){
if(annotation instanceof MyAnnotation){
MyAnnotation myAnnotation = (MyAnnotation) annotation;
System.out.println(myAnnotation.value()); // 輸出元註解
}
}
}
}
五、高級特性
1、反射機制
public class ReflectionDemo {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
// 獲取類的Class對象
Class clazz = Class.forName("com.reflection.Test");
Object object = clazz.newInstance();
// 獲取類的構造方法
Constructor constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
Object object1 = constructor.newInstance();
// 獲取類的私有方法
Method method = clazz.getDeclaredMethod("privateMethod", String.class);
method.setAccessible(true);
method.invoke(object, "JAVA高級編程開發技巧");
// 獲取類的欄位
Field field = clazz.getDeclaredField("name");
field.setAccessible(true);
field.set(object1, "reflnation");
System.out.println(field.get(object1));
}
}
class Test {
private String name;
public Test(){
this.name = "Java";
}
private void privateMethod(String msg){
System.out.println("私有方法:" + msg);
}
}
2、動態代理
public interface Service {
public void add();
public void update();
}
public class ServiceImpl implements Service{
public void add() {
System.out.println("添加用戶");
}
public void update() {
System.out.println("修改用戶");
}
}
public class ProxyDemo {
public static void main(String[] args) {
Service service = new ServiceImpl();
Service proxy = (Service) Proxy.newProxyInstance(service.getClass().getClassLoader(), service.getClass().getInterfaces(), new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(method.getName() + "開始執行");
Object result = method.invoke(service, args);
System.out.println(method.getName() + "執行完成");
return result;
}
});
proxy.add();
proxy.update();
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/312948.html
微信掃一掃
支付寶掃一掃