Java多软件线程始终使用主线程
这是我第一次使用这个网站。。。只要告诉我我做错了什么 Verk3a等级: RunMeSumThread类 等等Java多软件线程始终使用主线程,java,multithreading,Java,Multithreading,这是我第一次使用这个网站。。。只要告诉我我做错了什么 Verk3a等级: RunMeSumThread类 等等 我试图让它同时运行多个线程。我做错了什么?您应该实现线程在void run方法中执行的代码。只需将逻辑从类构造函数移到run方法中。下面是一个例子: public class RunMeSumThread extends Thread implements Runnable{ int n; public RunMeSumThread(int n) {
我试图让它同时运行多个线程。我做错了什么?您应该实现线程在void run方法中执行的代码。只需将逻辑从类构造函数移到run方法中。下面是一个例子:
public class RunMeSumThread extends Thread implements Runnable{
int n;
public RunMeSumThread(int n) {
this.n = n;
}
@Override
public void run() {
for (int i = 0; i < n; i++) {
System.out.print(i);
System.out.print(" | ");
System.out.print(Thread.currentThread().getName());
System.out.println();
}
}
}
您应该实现线程在void run方法中执行的代码。只需将逻辑从类构造函数移到run方法中。下面是一个例子:
public class RunMeSumThread extends Thread implements Runnable{
int n;
public RunMeSumThread(int n) {
this.n = n;
}
@Override
public void run() {
for (int i = 0; i < n; i++) {
System.out.print(i);
System.out.print(" | ");
System.out.print(Thread.currentThread().getName());
System.out.println();
}
}
}
在调用线程的start方法之前,不会在操作系统级别创建新线程。由于您在构造函数中打印,因此它总是在主线程上执行。在调用线程的start方法之前,不会在操作系统级别上真正创建新线程。因为您在构造函数中打印,所以它总是在主线程上执行。您已经在构造函数中添加了这些打印
System.out.print(i);
System.out.print(" | ");
System.out.print(Thread.currentThread().getName());
System.out.println();
因此,它将在创建对象时打印输出
注意:在调用方法start之前,它是一个简单的java对象,您已经在构造函数中添加了这些打印
System.out.print(i);
System.out.print(" | ");
System.out.print(Thread.currentThread().getName());
System.out.println();
因此,它将在创建对象时打印输出
注意:在调用方法start之前,一个简单的java对象应将Runnable的实现传递给线程的超级构造函数:
应将Runnable的实现传递给线程的超级构造函数:
您正在构造函数中执行println语句。构造函数正在主线程上执行。您正在构造函数中执行println语句。构造函数正在主线程上执行。这可能不起作用,因为从Runnable运行不会接受任何参数。@JoeInner你说得对。快速键入问题:。答案已编辑。这可能不起作用,因为从Runnable运行不会接受任何参数。@JoeInner你说得对。快速键入问题:。回答编辑好了,大人,这很有效。不仅如此,这看起来像是我老师教我的代码。。。别误会我。。。我这学期不及格,正在努力掌握我下学期的弱点。。。线程。。。我想学,但我需要更多的时间:谢谢你,谢谢你,亲吻:我的主,这成功了。不仅如此,这看起来像是我老师教我的代码。。。别误会我。。。我这学期不及格,正在努力掌握我下学期的弱点。。。线程。。。我想学,但我需要更多的时间:谢谢,谢谢,亲吻:谢谢
public class RunMeSumThread implements Runnable{
int n;
public RunMeSumThread(int n) {
this.n = n;
}
@Override
public void run() {
for (int i = 0; i < n; i++) {
System.out.print(i);
System.out.print(" | ");
System.out.print(Thread.currentThread().getName());
System.out.println();
}
}
}
public class Verk3a {
public static int n = 100;
public static void main(String[] args) {
//create a Thread and pass an instance of the class implementing Runnable here
Thread zack = new Thread(new RunMeSumThread(n));
Thread john = new Thread(new RunMeSumThread(n));
Thread konni = new Thread(new RunMeSumThread(n));
zack.start();
john.start();
konni.start();
}
}
System.out.print(i);
System.out.print(" | ");
System.out.print(Thread.currentThread().getName());
System.out.println();
public class RunMeSumThread extends Thread
{
public RunMeSumThread(final int n)
{
super(new Runnable()
{
@Override
public void run()
{
for (int i = 0; i < n; i++)
{
System.out.print(i);
System.out.print(" | ");
System.out.print(Thread.currentThread().getName());
System.out.println();
}
}
});
}
}