객체 지향 프로그래밍(Java) 실험 10

실험 내용 및 요구 사항

1. 실험 목적

  1. Java 다중 스레드 프로그래밍 방법을 마스터하십시오.
    2. 실험 콘텐츠
    컴퓨터에서 다음 프로그램을 구현하고 프로그램의 실행 조건을 관찰합니다.
  2. 새 스레드에서 정수의 계승 계산을 완료하는 스레드 프로그램을 작성하십시오. Thread 클래스와 Runnable 인터페이스를 각각 사용하여 달성하십시오.

실험 코드

Thread클래스 구현

package test10;
public class MyThread1 extends Thread{
    
    
    private int num;
    public void run(){
    
    
        int result = 1;
        for (int i=2;i<=num;i++){
    
    
            result *= i;
        }
        System.out.println(result);
    }
    MyThread1(int data){
    
    
        num = data;
    }
}

Runnable인터페이스 구현

package test10;

public class MyThread2 implements Runnable {
    
    
    private int value;
    MyThread2(int Value){
    
    
        value=Value;
        }
    public void start() {
    
    
        System.out.print(com(value));
    }
    public int com(int x){
    
    
        int result=1;
        for (int i = x; i > 1; i--) {
    
    
            result*=i;
            }
        return result;
    }
    @Override
    public void run() {
    
    
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
}

验证主程序

package test10;
import java.util.Scanner;
public class mainAct {
    
    
    public static void main(String [] args) {
    
    
        Scanner input = new Scanner(System.in);
        MyThread1 one = new MyThread1(input.nextInt());
        one.run();

        MyThread2 two = new MyThread2(input.nextInt());
        two.start();

    }
}

여기에 이미지 설명 삽입

추천

출처blog.csdn.net/qq_51594676/article/details/124995965