Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为我创建的线程对象调用方法,但不起作用。没有错误_Java_Multithreading_Methods_Constructor_Boolean - Fatal编程技术网

Java 为我创建的线程对象调用方法,但不起作用。没有错误

Java 为我创建的线程对象调用方法,但不起作用。没有错误,java,multithreading,methods,constructor,boolean,Java,Multithreading,Methods,Constructor,Boolean,这是密码 import java.util.Scanner; class Draft extends Thread { String name; static int counter; boolean islive; //... // the constructor public Draft(String name, int counter, boolean islive) { this.name = name; t

这是密码

import java.util.Scanner;

class Draft extends Thread
{

    String name;
    static int counter;
    boolean islive; //...

//  the constructor
    public Draft(String name, int counter, boolean islive)
    {
        this.name = name;
        this.counter = counter;
        this.islive = islive;   //...
    }

    void stopTheThread()
    {
        this.islive = false;    //...
    }

    @Override
    public void run()
    {
        while(islive)   //...
        {
            counter++;
            try
            {
                sleep(913);
            }
            catch (InterruptedException e) { } 
        }
    }

//  the main function
    public static void main(String[] args)
    {
//      intializing two threads
        Draft Tone = new Draft("Tone", 13, true);
        Draft Ttwo = new Draft("Ttwo", 26, true);

        System.out.println("All Thread Started!");
        Tone.start();    Ttwo.start();

        Scanner scan = new Scanner(System.in);
        String go = "";

//      the while loop.......
        while(!go.toLowerCase().equals("exit")) {

            System.out.println();   //just a new line
            System.out.print("Command: ");
            go = scan.next();

            if(go.equals("1")) {
                System.out.print("Thread one "+ Tone.counter);
            }
            else if(go.equals("2")) {
                System.out.print("Thread wo: "+ Ttwo.counter);
            } 
            else if(go.equals("1Stop")) {
                Tone.stopTheThread();   //is not stoping
                System.out.print("Thread one Stopped"); //but printing
            } 
            else if(go.equals("2Stop")) {
                Ttwo.stopTheThread();   //is not stopping too
                System.out.print("Thread two Stopped"); //printing too
            }

        }//end of the while

    }//end of main

}   //end of Draft
我为我的Draft类扩展了Thread类。 在run方法中,线程主要在isalive条件下的while循环上运行,这是一个布尔值

我创建了一个方法stopTheThread,它将isalive设置为false,并且运行时循环方法应该停止。 但实际上,当我在我的主要方法中调用它时,它并没有停止。两者都是在main方法上创建的双线程对象

所有条件都已检查


我在那里做错了什么?

看起来两个线程都在递增相同的静态计数器变量,所以它们都在运行并递增相同的变量。
我测试了代码并按预期工作,计数器一直递增,直到我“停止”两个线程,如果希望每个线程递增自己的变量,应该删除静态修饰符。

但我创建了两个不同的对象。音调和Ttwo。每个对象的计数器应延迟。那么为什么两个线程都会增加相同的计数器。我不明白。你能再解释一下吗?你为什么在这里提到静电?如果我去除静电,它就会工作。为什么?@Rabin:你认为,
static
是什么意思?@Rabin static意味着,无论创建了多少个包含类(草稿)的实例,都会有一个共享的唯一实例,每个实例都会共享同一个静态对象(计数器)。@DanielRodriguez所以我的两线程实例使用的是同一个计数器。这就是为什么当我停止一个线程时,计数器仍然在增加另一个线程,直到我也停止那个线程。这是静电唯一能做的事吗?为什么我们在主方法中通常使用静态?你能解释一下吗?