这是我的代码,它处理线程。我在 run() 中遇到问题,因此我无法编译它。如果有人知道如何使用 DateFormat 参数调用方法,请告诉我。
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
class Base implements Runnable {
static DateFormat format =
DateFormat.getDateInstance(DateFormat.MEDIUM);
public Date parse(String str) throws ParseException {
synchronized (getClass()) {
return format.parse(str);
}
}
@Override
public void run() {
/*Date date = new Date(111111);
DateFormat dateF = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
date.getDateInstance(dateF);*/
parse("Hello");
}
}
class Derived extends Base implements Runnable{
public Date doSomethingAndParse(String str) throws ParseException {
synchronized(Base.class) {
System.out.println("Derived Class");
return format.parse(str);
}
}
public static void main(String[] args) {
Derived d = new Derived();
Thread t = new Thread(d);
Thread t2= new Thread (d);
t.start();
t2.start();
}
@Override
public void run() {
getClass();
try {
doSomethingAndParse("1111111111");
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("Run in Derived Class");
}
}