多线程相关作业(JAVA):
还是那句话照抄的话改一下变量名、输出提示什么的的的的的的的!!!然后去创建项目运行一下
还有关注!!!
运行结果:
import java.util.HashMap;
import java.util.Map;
public class xiejing_work5 {
enum Month{
JANUARY("JANUARY"),
FEBRUARY("FEBRUARY"),
MARCH("MARCH"),
APRIL("APRIL"),
MAY("MAY");
private String month;
private Month(String Month) {
this.month = Month;
}
private void putMonth() {
System.out.println(this.month);
}
}
static Map events = new HashMap<String, Month>();
public static void main(String[]args) {
Thread thread1 = new Thread() {
public void run() {
events.put("1", Month.JANUARY);
events.put("2", Month.FEBRUARY);
events.put("3", Month.MARCH);
events.put("4", Month.APRIL);
events.put("5", Month.MAY);
events.put("6", Month.JANUARY);
events.put("7", Month.FEBRUARY);
events.put("8", Month.MARCH);
events.put("9", Month.APRIL);
events.put("10", Month.MAY);
try {
System.out.println("thread1休眠两秒。");
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("thread1休眠结束。");
events.put("11", Month.APRIL);
events.put("12", Month.MAY);
events.put("13", Month.JANUARY);
events.put("14", Month.FEBRUARY);
}
};
Thread thread2 = new Thread() {
public void run() {
Month temp;
while(true) {
if(events.keySet().size() != 0) {
for(Object i : events.keySet()) {
System.out.println("thread2正在执行第" + i.toString() + "个任务");
temp = (Month)events.get(i.toString());
temp.putMonth();
events.remove(i.toString());
break;
}
}
System.out.println("thread2正在等待任务");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
thread2.start();
thread1.start();
}
}