选择两个城市作为预选旅游目标,实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000毫秒以内),哪个先显示完毕,就决定去哪个城市。请用Runnable接口或Thread类实现。下面是用Thread类的实现,参考程序如下,用Runable接口实现方法类似。
public class Ex82 {
public static void main(String[] args){
String[] citys = {"合肥","淮南"};
ThreadCity1 thread1 = new ThreadCity1(citys);
ThreadCity2 thread2 = new ThreadCity2(citys);
thread1.start();
thread2.start();
/*
* 请在下面补充几行代码,保证thread1和thread2都允许结束再执行后面的if语句。
*/
While(thread1.isAlive() || thread2.isAlive()){
}
if(thread1.getTime()<thread2.getTime()){
System.+
out.println("决定去:"+citys[0]);
}
else{
System.out.println("决定去:"+citys[1]);
}
}
}
class ThreadCity1 extends Thread{
private String[] citys;
private long time;
public long getTime(){
return time;
}
ThreadCity1(String[] citys){
this.citys=citys;
}
public void run(){
long start = System.currentTimeMillis();
for(int i=0;i<10;i++){
try{
System.out.println("Thread1:"+citys[0]);
Thread.sleep((int)(Math.random()*1000));
}
catch(Exception e){
}
}
time = System.currentTimeMillis() - start;
System.out.println("Thread1 Ends!");
}
}class ThreadCity2 extends Thread{
private String[] citys;
private long time;
public long getTime(){
return time;
}
ThreadCity2(String[] citys){
this.citys=citys;
}
public void run(){
long start = System.currentTimeMillis();
for(int i=0;i<10;i++){
try{
System.out.println("Thread2:"+citys[1]);
Thread.sleep((int)(Math.random()*1000));
}
catch(Exception e){
}
}
time = System.currentTimeMillis() - start;
System.out.println("Thread2 Ends!");
}
}
public class Ex82 {
public static void main(String[] args){
String[] citys = {"合肥","淮南"};
ThreadCity1 thread1 = new ThreadCity1(citys);
ThreadCity2 thread2 = new ThreadCity2(citys);
thread1.start();
thread2.start();
/*
* 请在下面补充几行代码,保证thread1和thread2都允许结束再执行后面的if语句。
*/
While(thread1.isAlive() || thread2.isAlive()){
}
if(thread1.getTime()<thread2.getTime()){
System.+
out.println("决定去:"+citys[0]);
}
else{
System.out.println("决定去:"+citys[1]);
}
}
}
class ThreadCity1 extends Thread{
private String[] citys;
private long time;
public long getTime(){
return time;
}
ThreadCity1(String[] citys){
this.citys=citys;
}
public void run(){
long start = System.currentTimeMillis();
for(int i=0;i<10;i++){
try{
System.out.println("Thread1:"+citys[0]);
Thread.sleep((int)(Math.random()*1000));
}
catch(Exception e){
}
}
time = System.currentTimeMillis() - start;
System.out.println("Thread1 Ends!");
}
}class ThreadCity2 extends Thread{
private String[] citys;
private long time;
public long getTime(){
return time;
}
ThreadCity2(String[] citys){
this.citys=citys;
}
public void run(){
long start = System.currentTimeMillis();
for(int i=0;i<10;i++){
try{
System.out.println("Thread2:"+citys[1]);
Thread.sleep((int)(Math.random()*1000));
}
catch(Exception e){
}
}
time = System.currentTimeMillis() - start;
System.out.println("Thread2 Ends!");
}
}
解决方案 »
- 同样的程序在WINDOWS上和LINUX上差别那么大?
- =======帮忙解决个关于远程控制方面的问题,100分给你=======
- 一个关于Unknown Source的问题,谢谢解答!
- 还是Runtime问题请教!!!
- 急急急,壓縮 有中文名的文件 後 在壓縮文件中無法正常顯示中文 文件名 幫忙看一下
- 比较奇怪的一个异常...
- 关于JAR的问题。请高手回答
- 如何设置一个计数器用来计数当前程序的运行状态?要求此程序不用多线程来编制的。
- 在java应用程序中,如何验证输入为时间格式。
- 问一个比较弱的问题,内存泄露的定义是什么?
- java swing 中给table添加右键菜单
- 关于java中对象的内存中的分配,新手求助
举个例子
public class ProcessDemo { public static void main(String[] args) {
try {
// create a new process
System.out.println("Creating Process...");
Process p = Runtime.getRuntime().exec("notepad.exe"); // cause this process to stop until process p is terminated
p.waitFor(); // when you manually close notepad.exe program will continue here
System.out.println("Waiting over."); } catch (Exception ex) {
ex.printStackTrace();
} }
}