java - Java多线程基本控制台程序

标签 java multithreading

我是多线程新手。因此,这就是我正在处理的问题:将学生证的后四位存储在两个单独的类(class)中。例如,如果您的ID是SE123456789,则将67存储在第一个Thread1类中,将89存储在Thread2类中,稍后再传递给阶乘类。
Thread1类会将67传递给阶乘类,并且printfactorial()将打印从1到67的所有数字的阶乘。
Thread2类会将89传递给阶乘类,并且printfactorial()将打印从1到89的所有数字的阶乘。
您应在循环内的每次计算之后使用thread.sleep(10)。
如您所知,您不能在简单的整数或长型变量中存储和打印大数的阶乘,因此您需要使用BigInteger来存储和打印非常长的数字。
必须同步printfactorial()方法,以便首先打印Thread1的结果,然后计算并打印Thread2的结果。
到目前为止,这是我所做的。
我有四个不同的类(class)

Main
Factorial
Thread1
Thread2
Thread1和Thread2都扩展了Thread类。
这是我到目前为止编写的代码:

Main

public class Main {
 public static void main(String args[]){
    Factorial factorial = new Factorial();  
    Thread1 t1 = new Thread1(factorial);
    Thread1 t2 = new Thread1(factorial);
    t1.start();
    t2.start();
 }
}

Factorial Class

import java.math.BigInteger;
public class Factorial {
    public void printFactorial(int number){
        BigInteger bigInteger = new BigInteger("1");
        try{
            for(int i=1; i<=number; i++){
                bigInteger = bigInteger.multiply(BigInteger.valueOf(i));
                Thread.sleep(10); 
                System.out.println(bigInteger);
            }
        }catch(InterruptedException ex){
            System.out.println("the interruption has occurred in the thread");
        }
    }
}

Thread1

package com.mycompany.factorial;
public class Thread1 extends Thread {
    Factorial factorial;
    Thread1(Factorial fact){
        factorial = fact;
    }
    @Override
    public void start(){
        synchronized(factorial){
            try{
                /*my ID is: SE170400080
                so the second last two digits are 00.
                **/
                factorial.printFactorial(00); //here's the problem
            }catch(Exception e){
                System.out.println("the interruption has occurred in the thread");
            }
        }
    }
}

Thread2

package com.mycompany.factorial;

public class Thread2 extends Thread {
    Factorial factorial;
    Thread2(Factorial fact){
        factorial = fact;
    }
    @Override
    public void start(){
        synchronized(factorial){
            try{
                factorial.printFactorial(80);
              
            }catch(Exception e){
                System.out.println("the interruption has occurred in the thread");
            }
        }
    }
}
运行主程序后,它会成功构建,但未显示所需的输出。
enter image description here
我们将不胜感激帮助,我尽力保持其准确性。
1:

最佳答案

您的代码中有一些错误:

  • 覆盖run方法,而不是start:start仅启动您的线程,但是实际运行的代码属于run方法。在Oracle的website上有一个不错的小教程。
  • main中,您创建Thread1的两个实例,这可能是复制粘贴错误
  • 关于java - Java多线程基本控制台程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63087308/

    相关文章:

    c++ - localtime 不是线程安全的,但是可以只调用一个线程吗?

    java - Selenium - 如何选择具有可变文本的元素

    .net - 使用互锁

    java - 作为 JComponent 或 JPanel 的 JTable 工具提示

    java - Flink 滑动窗口没有按预期工作

    c++ - 是否有任何惯用的显式使用 mutex::lock() 或 unlock()?

    c++ - <mutex> 和 <condition_variable> 的异常处理

    c - 程序的编译在执行前是否会在缓存中存储一​​些数据?? (C, Linux)

    java - 无法找出Java程序检查字符串是否回文的解决方案

    java - 如何列出当前目录中的文件?