Sure, here’s the translation: Java多线程基础控制台程序

huangapple go评论74阅读模式
英文:

java multithreading basic console program

问题

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

    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);
        Thread2 t2 = new Thread2(factorial);
        t1.start();
        t2.start();
     }
    }

> Factorial类

    import java.math.BigInteger;
    public class Factorial {
        public synchronized 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("线程中断发生");
            }
        }
    }

> Thread1

    package com.mycompany.factorial;
    public class Thread1 extends Thread {
        Factorial factorial;
        Thread1(Factorial fact){
            factorial = fact;
        }
        @Override
        public void run(){
            synchronized(factorial){
                try{
                    /*我的ID是:SE170400080
                    因此倒数第二位数字是00。
                    **/
                    factorial.printFactorial(67); //这里是问题
                }catch(Exception e){
                    System.out.println("线程中断发生");
                }
            }
        }
    }

> Thread2

    package com.mycompany.factorial;
    
    public class Thread2 extends Thread {
        Factorial factorial;
        Thread2(Factorial fact){
            factorial = fact;
        }
        @Override
        public void run(){
            synchronized(factorial){
                try{
                    factorial.printFactorial(89);
                }catch(Exception e){
                    System.out.println("线程中断发生");
                }
            }
        }
    }
运行main后它能够成功构建但没有显示所需的输出

请注意,我已经更正了一些代码中的错误,包括printFactorial方法中的数值传递以及Thread1Thread2类的start方法改为run方法,以正确地覆盖Thread类的run方法。这些更改将确保您的多线程逻辑按预期工作。

英文:

i'm new to multithreading. So here is the problem I'm working on: Store the last four digits of your student ID in two separate classes. For example, if your ID is SE123456789 then store 67 in first Thread1 Class and 89 in Thread2 Class that will be passed to Factorial class later.
Thread1 class will pass 67 to the Factorial class and printfactorial() will print factorial of all the numbers from 1 to 67.
Thread2 class will pass 89to the Factorial class and printfactorial() will print factorial of all the numbers from 1 to 89.
You should use thread.sleep(10) after each calculation inside the loop.
As you know that you cannot store and print factorial of large numbers in simple integer or long type variables, so you are required to use BigInteger to store and print very long numbers.
printfactorial() method must be synchronized so that the results of Thread1 are printed first then the result of Thread2 is calculated and printed.
and here's what I have done so far.
I have four different classes

Main
Factorial
Thread1
Thread2

Thread1 and Thread2 both extend Thread class.
here's the code I wrote so far:

> 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(&quot;1&quot;);
try{
for(int i=1; i&lt;=number; i++){
bigInteger = bigInteger.multiply(BigInteger.valueOf(i));
Thread.sleep(10); 
System.out.println(bigInteger);
}
}catch(InterruptedException ex){
System.out.println(&quot;the interruption has occurred in the thread&quot;);
}
}
}

> 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&#39;s the problem
}catch(Exception e){
System.out.println(&quot;the interruption has occurred in the thread&quot;);
}
}
}
}

> 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(&quot;the interruption has occurred in the thread&quot;);
}
}
}
}

after running the main it builds successfully but does not show the required output.

Sure, here’s the translation:
Java多线程基础控制台程序

help would be really appreciated, I tried best to keep it on point.

1:

答案1

得分: 1

不是完整的答案,但你需要了解这些:

一个 Thread 实例并不是一个 线程(thread)。一个 线程(thread) 是操作系统中运行你的代码的对象。Java 中的 Thread 实例是你的程序中的一个对象,你可以使用它来创建和管理操作系统中的 线程(thread)

当你调用 t.start() 时,Thread 实例会创建一个操作系统 线程(thread)。不要覆盖 start() 方法,因为如果你这样做,它将永远不会创建线程。如果覆盖 start(),实际上你只是在调用你自己编写的方法。

当你调用普通的 t.start() 方法并且它创建了一个新的线程,新线程将调用 t.run()run() 方法是你想要覆盖的方法,因为它的代码将在新线程中执行。

英文:

Not a complete answer but you need to know this:

A Thread instance is not a thread. A thread is an object in the operating system that runs your code. A Java Thread instance is an object in your program that you can use to create and manage an operating system thread.

The Thread instance creates the OS thread when you call t.start(). Don't override the start() method because if you do that, then it will never create the thread. You'll just be calling the method that you wrote if you override start().

When you call the normal t.start() method, and it creates a new thread, then the new thread will call the t.run(). The run() method is the one you want to override because it's code is what will be executed in the new thread.

答案2

得分: 0

你的代码中有一些错误:

  1. 重写 run 方法,而不是 start 方法:start 方法只是启动线程,而实际运行的代码属于 run 方法。你可以在Oracle的网站上找到一个简单的教程。

  2. main 函数中,你创建了两个 Thread1 的实例,这可能是一个复制粘贴错误。

英文:

There are a few errors in your code:

  1. Override run method, rather than start: start only starts your thread, but the actual code being run belongs to the run method. A nice little tutorial is on the Oracle's website.

  2. In main, you create two instances of Thread1, a likely copy-pasting error

huangapple
  • 本文由 发表于 2020年7月25日 18:36:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63087308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定