JAVA程序设计进阶_1_创建线程

第一关

编程要求

本关一共2个文件Main.java与MyThread.java。
其中Main.java文件不允许编辑,其内容如下
package step1;
import java.util.Scanner;
import java.lang.Thread;
public class Main{
public static void main(String[]args) {
//输入一个整数
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
//用来标记主线程开始
System.out.println(“Main Thread starts.”);
//MyThread是Thread的派生类,而thread是MyThread的实例
Thread thread = new MyThread(n);
//执行线程
thread.start();
//标记主线程结束
System.out.println(“Main Thread ends.”);
cin.close();
return;
}
}
其主要功能是输入一个整数,计算阶乘并输入。
特别的,要求阶乘的输出在主线程结束之后,也就是阶乘输出必须在”Main Thread ends.”这一行之后。
通过创建一个子线程来实现这一点。子线程的实例化及执行代码已写在Main.java中。用户需要补全MyThread.java中的内容,创建一个完整的MyThread类以配合Main.java满足任务要求。

参考代码

package step1;
import java.lang.Thread;

public class MyThread extends Thread {
	private int num;//任务就是在子线程中计算num的阶乘
	
	public MyThread() {
		this(0);
	}
	
	//constructor,创建实例的时候初始化参数
	public MyThread(int num) {
		/***begin your code here***/ 
		this.num=num;
		/***end your code***/
	}
	
    @Override
    public void run() {    	
    	//重写run方法,在子线程中想要执行的代码写在run方法中
    	int result = 1;//result保存计算出的结果
    	
        /***begin your code here***/ 
	    int i;
    	for (i = num; i > 0; --i)
    		result *= i;
		/***end your code***/
    	
    	//直接输出结果
    	System.out.println(result);
    }
}


第二关

编程要求

本关一共2个文件Main.java与MyRunnable.java。
其中Main.java文件不允许编辑,其内容如下
package step2;
import java.util.Scanner;
import java.lang.Thread;
public class Main{
public static void main(String[]args) {
//输入一个整数
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
//用来标记主线程开始
System.out.println(“Main Thread starts.”);
//MyRunnable实现了Runnable接口
MyRunnable r = new MyRunnable(n);
//使用MyRunnable的实例去实例化一个Thread
Thread thread = new Thread®;
//执行线程
thread.start();
//标记主线程结束
System.out.println(“Main Thread ends.”);
cin.close();
return;
}
}
其主要功能是输入一个整数,计算阶乘并输入。
特别的,要求阶乘的输出在主线程结束之后,也就是阶乘输出必须在”Main Thread ends.”这一行之后。
通过创建一个子线程来实现这一点。该子线程通过一个Runnable参数创建。创建及执行代码已在Main.java中完成。用户需要补全MyRunnable.java中的内容以配合Main.java满足任务要求。

参考代码

package step2;

public class MyRunnable implements Runnable {
	private int num;
	
    public MyRunnable() {
    	this(0);
    }
    
    public MyRunnable(int num) {
    	/***begin your code here***/
    	this.num=num;
    	/***end your code here***/
    }
	
	@Override
	public void run() {
		//重写run方法,在子线程中想要执行的代码写在run方法中
    	int result = 1;//result保存计算出的结果
    	
        /***begin your code here***/ 
		int i;
        for (i = num; i > 0; --i)
        result *=i;
		/***end your code***/
    	
    	//直接输出结果
    	System.out.println(result);
	}

}

第三关

编程要求

本关一共2个文件Main.java与ThreadHelper.java。
其中Main.java文件不允许编辑,其内容如下
package step3;
import java.util.Scanner;
public class Main{
public static void main(String[]args) {
//输入一个整数
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
//用来标记主线程开始
System.out.println(“Main Thread starts.”);
//调用一个静态方法来计算阶乘
ThreadHelper.calcOnNewThread(n);
//标记主线程结束
System.out.println(“Main Thread ends.”);
cin.close();
return;
}
}
其主要功能是输入一个整数,计算阶乘并输入。
特别的,要求阶乘的输出在主线程结束之后,也就是阶乘输出必须在”Main Thread ends.”这一行之后。
通过调用一个静态方法来实现这一点。用户需要补全ThreadHelper.java中的内容。实际上使用派生了或者实现Runnable接口都能实现本关的任务。但是希望用户能够使用匿名对象来实现。并且可以观察Java工具类的某些特征。

参考代码

package step3;

/**
 * 模拟一个工具类,该类不用来实例化,只提供各种静态函数
 * @author Administrator
 *
 */
public class ThreadHelper {
	/**
	 * 在子线程中计算参数的阶乘并输出
	 * @param num
	 */
    static public void calcOnNewThread(int num) {
    	//使用Thread匿名对象以及Runnable匿名对象创建并执行子线程
    	new Thread(new Runnable() {
    	    @Override public void run() {
    	    	int result = 1;
    	    	/***begin your code here***/ 
                int i;
    			for (i = 1; i<=num; ++i)
                result *=i;
    			/***end your code***/
    	    	//直接输出
    	    	System.out.println(result);
    	    }		
    	}).start();
    }
    
    /**
     * 使用一个私有的构造器,防止该类被实例化
     */
    private ThreadHelper() {
    	throw new UnsupportedOperationException(this.getClass().getSimpleName() + " can not be instantiated.");
    }
}

第四关

编程要求

本关一共4个文件Main.java、MyThread.java、MyRunnable.java和ThreadHelper.java。
其中Main.java文件不允许编辑,其内容如下
package step4;
import java.util.Scanner;
public class Main{
public static void main(String[]args) {
//输入3个整数
Scanner cin = new Scanner(System.in);
int a = cin.nextInt();
int b = cin.nextInt();
int c = cin.nextInt();
//用来标记主线程开始
System.out.println(“Main Thread starts.”);
//分别使用3种形式执行子进程,尽可能使用匿名对象
new MyThread(a).start();
new Thread(new MyRunnable(b)).start();
ThreadHelper.calcOnNewThread©;
//标记主线程结束
System.out.println(“Main Thread ends.”);
cin.close();
return;
}
}
分别以a、b、c作为参数,以不同形式创建并执行线程。最后要求在主线程结束之后,依次输出c、b、a的阶乘。
其余3个文件均是空的,用户可以自由编写,只要能够配合Main.java完成任务。

参考代码

package step4;
//注意文件名,这里应该写MyRunnable类
/***begin your code here***/
public class MyRunnable implements Runnable {
	private int num;	
  
    public MyRunnable(int num) {
    	this.num=num;
    }
	@Override
	public void run() {
		//重写run方法,在子线程中想要执行的代码写在run方法中
    	int result = 1;//result保存计算出的结果
        int i;
        for (i=num;i>0;--i){
        result *=i;
        try{
            Thread.sleep(100);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        }
    	System.out.println(result);
	}

}

       
/***end your code***/

package step4; 
//注意文件名,这里应该写MyThread类
/***begin your code here***/
public class MyThread extends Thread {
	private int num;//任务就是在子线程中计算num的阶乘
    
	public MyThread(int num) {
		this.num=num;
	}

    @Override
    public void run() {    	
    	//重写run方法,在子线程中想要执行的代码写在run方法中
    	int result = 1;//result保存计算出的结果
        int i;
    	for (i=num;i>0;--i){
    		result *= i;
            try{
                Thread.sleep(1000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    	//直接输出结果
    	System.out.println(result); 
    }
}
/***end your code***/


package step4;

//注意文件名,这里应该写ThreadHelper类
/***begin your code here***/
public class ThreadHelper {
    static public void calcOnNewThread(int num) {
    	//使用Thread匿名对象以及Runnable匿名对象创建并执行子线程
    	new Thread(new Runnable() {
    	    @Override public void run() {
    	    	int result = 1;
                int i;
    			for (i=num;i>0;--i)
                result *=i;             
    	    	System.out.println(result);
    	    }		
    	}).start();
    }
}
/***end your code***/

临近放假,作业繁多,更新可能会慢,代码有误或者有更好的意见,多多交流!!!

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忘的比学的还快

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值