JAVA程序设计进阶(自主模式)-利用THREAD创建线程

版权声明:文章都是原创,转载请注明~~~~ https://blog.csdn.net/SourDumplings/article/details/88934333

利用THREAD创建线程

(100/100 分数)

题目描述

设计并实现一个Thread的子类,命名为MyThread。

MyThread类具有一个int类型的私有成员,由构造器传入。并且覆写run方法,在run方法中计算并输出私有成员的阶乘。

主方法将会根据输入创建MyThread的实例,并利用实例在新的子线程中计算输出给定输入的阶乘。

学员无需实现主方法,只需按照指示补全MyThread类即可。

本题中出现的所有数据均在int范围内。

本题不提供输入输出样例

Java:

import java.util.*;
public class Main{
    public static void main(String [] args){
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        cin.close();
          
        //创建一个MyThread的实例,并且在新的线程中执行
        new MyThread(n).start();
        System.out.println("Main thread ends.");
    }
}
  
class MyThread extends Thread{
    private int num;//私有成员
    //请在以下实现构造器并覆写run方法
    /***begin your code here***/
    public MyThread(int num)
    {
    	this.num = num;
    }
    
    public void run()
    {
    	int res = 1;
    	int i;
    	for (i = num; i > 0; --i)
    		res *= i;
    	System.out.println(res);
    }
    /***end your code***/
}
  

猜你喜欢

转载自blog.csdn.net/SourDumplings/article/details/88934333