class MyThread extends Thread {
public void run() {
System.out.println("线程正在执行");
}
}
// 使用 MyThread thread = new MyThread(); thread.start();
Thread thread = new Thread(() -> {

System.out.println("当前线程:" + Thread.currentThread().getName());
});
thread.start(); // 输出:Thread-0(新线程) // thread.run(); // 输出:main(主线程)

public synchronized void addBalance(int amount) {
balance += amount;
}
public class Counter {
private int value;
private final Object lock = new Object();
public void increment() {
synchronized(lock) {
value++;
}
}
public int get() {
synchronized(lock) {
return value;
}
}
}