public class MultiThreadExample {
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyRunnable());
        Thread t2 = new Thread(new MyRunnable());
        
        t1.start();
        t2.start();
        
        // 等待两个线程结束
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        System.out.println("多线程编程示例结束");
    }
}

class MyRunnable implements Runnable {
    public void run() {
        for (int i = 0; i< 10; i++) {
            System.out.println("当前线程: " + Thread.currentThread().getName() + ", 值: " + i);
        }
    }
}

Java异常处理的最佳实践

public class ExceptionHandlingExample {
    public static void main(String[] args) {
        try {
            int result = divide(10, 0);
            System.out.println("计算结果: " + result);
        } catch (ArithmeticException e) {
            System.out.println("除数为0,发生异常: " + e.getMessage());
        } finally {
            System.out.println("异常处理结束");
        }
    }
    
    public static int divide(int num1, int num2) {
        return num1 / num2;
    }
}

Java集合框架的应用场景

import java.util.*;

public class CollectionFrameworkExample {
    public static void main(String[] args) {
        List list = new ArrayList<>();
        list.add("Java");
        list.add("Python");
        list.add("C++");
        System.out.println("列表大小: " + list.size());
        
        Set set = new HashSet<>();
        set.add(10);
        set.add(20);
        set.add(30);
        System.out.println("集合大小: " + set.size());
        
        Map map = new HashMap<>();
        map.put("apple", 1);
        map.put("banana", 2);
        map.put("orange", 3);
        System.out.println("映射大小: " + map.size());
    }
}