英文:
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
方法中的数值传递以及Thread1
和Thread2
类的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("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");
}
}
}
}
after running the main it builds successfully but does not show the required output.
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
你的代码中有一些错误:
-
重写
run
方法,而不是start
方法:start
方法只是启动线程,而实际运行的代码属于run
方法。你可以在Oracle的网站上找到一个简单的教程。 -
在
main
函数中,你创建了两个Thread1
的实例,这可能是一个复制粘贴错误。
英文:
There are a few errors in your code:
-
Override
run
method, rather thanstart
:start
only starts your thread, but the actual code being run belongs to therun
method. A nice little tutorial is on the Oracle's website. -
In
main
, you create two instances ofThread1
, a likely copy-pasting error
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论