public class MyThread extends Thread {
public void run() {
System.out.println("MyThread.run()");
}
}
MyThread myThread1 = new MyThread();
myThread1.start();
2、实现Runnable接口:
public class MyThread immplements Runnable {
public void run(){
sysout.out.println("MyThread.run()");
}
}
MyThread runThread = new MyThread();
Thread runThread = new Thread(runThread,"");
runThread.start();
public void getName(Object o){
synchronized(o){
//TODO
}
}
这里表示锁住这个变量o;
这里做一个线程安全的单例模式
public class Car{
//构造方法私有化
private Car();
//创建一个静态的私有的空的常量car
private static Car car = null;
//对外开放一个静态的共有的方法用来获取实例
public static getInstance(){
if(car == null){
synchronized(Car.getClass()){
if(car == null){
car = new Car();
}
}
}
return car;
}
}