线程和public void run()

时间:2013-02-23 02:25:21

标签: java multithreading

import java.lang.Thread;

class ThreadExperiment implements Runnable {
  public static void main(String[] args) {
    Thread t = new Thread(new ThreadExperiment());
    @Override
    public void run(){      
        do {
            num ++;
            try {
                Thread.sleep(400);
            } catch (InterruptedException e){
            }
        } while (num >= 0);
    }

    Thread t2 = new Thread(new ThreadExperiment());
    @Override
    public void run() {
        do {
            num2--;
            try {
                Thread.sleep(400);
            } catch (InterruptedException e){
            }
        } while (num >= 1);
    }


    int num = 1;
    int num2 = 10;
    t.start();
    t2.start();
    if (num == num2) {
        t.interrupt();
        t2.interrupt();
    }
  }
}

尝试修补线程并围成一圈,总是如此接近。我想要两个线程,一个增加数量,另一个增加数量。如果他们相遇,我希望他们停下来。但是我遇到了公共无效运行问题() - 目前,它说我不对t have a semi-colon next to both of them. What isn对吗?这是放置public void run()的正确位置吗?

另外,有些网站说我需要两个单独的类来创建线程 - 这有什么原因吗?也许如果一个线程依赖于另一个线程的计算但是走另一条线路,我可以理解,但我感觉不同,只是两个独立的实体。

最后,我需要import语句吗?

3 个答案:

答案 0 :(得分:1)

首先,你的代码看起来应该有很多编译错误,这对我来说可能会以错误的方式进行编码。如果你不能使用IDE,那么你必须早期编译,而不是任何新的代码,直到所有编译错误得到修复。

例如,您似乎有一个方法,运行,嵌入另一个方法main,而您根本无法做到这一点。我建议你重新开始,从一个编译的小代码框架开始,然后再次经常编译。还可以获得主方法的大部分代码之外。不,run方法不在正确的位置,因为你应该只有一个 run方法,它应该在类本身,不嵌入main或任何其他方法。

关于,

  

最后,我需要import语句吗?

你的编译器会告诉你:摆脱导入,看看会发生什么。

答案 1 :(得分:1)

1)。您希望编译代码。

您的编译问题是因为您要在main方法中声明您的run方法。将方法声明分开,然后使用main创建&运行你的线程。

2)。您希望同时运行两个线程。

这意味着我们需要定义两个不同的线程类或一个带有逻辑的线程来处理递增和递减。 Andrew Mao的回答为您提供了如何定义两个独立线程的开始。我上面的代码对run方法使用单个定义,该方法使用参数来确定要走的路。

3)。您希望在同一数据上同时运行两个线程,因为当它们分别递增/递减时,它们需要检查冲突。

一种简单的方法是在main方法中创建要处理的对象(num1和num2),然后将对这些对象的引用传递给线程。上面代码中的示例。

4)。你想测试一下

所有java.lang。*都假设是自动导入的。为了清楚起见,将它放在那里没有坏处,但是你选择的开发工具上的自动导入器命令可能会将其删除为冗余。

public class ThreadExperiment implements Runnable {
    /* these fields are unique to each instance of ThreadExperiment */
    private int increment = 0;

    /* these are used to point to the original num1 and num2 instances created in your main method */ 
    private Integer myNumber;
    private Integer theOtherNumber;


/** 
 * Constructor.   
 */
public ThreadExperiment(int increment, Integer num1Ref, Integer num2Ref) {
    this.increment = increment;
    this.myNumber = num1Ref;
    this.theOtherNumber = num2Ref;
}


@Override
public void run() {
    do {
        myNumber += increment;
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
        }
        System.out.println(Thread.currentThread().getName() + " -- " + myNumber);
    } while (!myNumber.equals(theOtherNumber));
}


/** 
 * Your static main method used to instantiate & start threads 
 */
public static void main(String[] args) {
    Integer num1 = 0;
    Integer num2 = 10;

    Thread t = new Thread(new ThreadExperiment(1, num1, num2), "Thread 1");
    Thread t2 = new Thread(new ThreadExperiment(-1, num2, num1), "Thread 2");
    t.start();
    t2.start();
}

}

答案 2 :(得分:0)

线程实现的run方法需要位于单独的类中。你当然可以把它们中的一个放在你的ThreadExperiment课堂上,但不能两者兼得。在这种情况下,您可以将两个线程分成内部类:

class ThreadExperiment {

  static int num = 1;
  static int num2 = 10;

  class Thread1 implements Runnable {
    @Override
    public void run(){      
        do {
            num++;
            try {
                Thread.sleep(400);
            } catch (InterruptedException e){
            }
        } while (num >= 0);
    }
  }

  class Thread2 implements Runnable {
    @Override
    public void run() {
        do {
            num2--;
            try {
                Thread.sleep(400);
            } catch (InterruptedException e){
            }
        } while (num >= 1);
    }
  }

  public static void main(String[] args) {

    Thread t = new Thread(new Thread1());    
    Thread t2 = new Thread(new Thread2());    

    t.start();
    t2.start();

    if (num == num2) {
        t.interrupt();
        t2.interrupt();
    }
  }
}

现在我们已经到了某个地方,但您仍然需要修复逻辑上的一些问题:

  • int作为静态不是好习惯。 (但这种测试方案没问题。)
  • 您的num == num2检查只会发生一次,并且无法保证在发生这种情况时值将会是什么。线程不太可能被中断。
  • 您需要声明int s volatile,因为它们将被不同的线程读取。
  • while中的Thread1条件将导致无限循环。
相关问题