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 - Fatal编程技术网

Java 如何在静态类中使用线程实现停止条件

Java 如何在静态类中使用线程实现停止条件,java,multithreading,Java,Multithreading,我正在做一个项目,我在停止这个项目时遇到了一些困难。我使用线程而不是计时器,因为我觉得它更容易使用。基本上,我现在的问题是把时间从主函数转移到静态函数。任何帮助都将不胜感激。如果我的问题不清楚,我将代码的重要部分包括在注释中。短暂性脑缺血发作 import java.util.Random; import java.util.Scanner; import javax.swing.JOptionPane; public class InLineCustomers { @Suppres

我正在做一个项目,我在停止这个项目时遇到了一些困难。我使用线程而不是计时器,因为我觉得它更容易使用。基本上,我现在的问题是把时间从主函数转移到静态函数。任何帮助都将不胜感激。如果我的问题不清楚,我将代码的重要部分包括在注释中。短暂性脑缺血发作

import java.util.Random;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class InLineCustomers {
    @SuppressWarnings("static-access")
    public static void main (String args[]){
        try{
            final long NANOSEC_PER_SEC = 1000l*1000*1000;

            long startTime = System.nanoTime();
            long time = (System.nanoTime()-startTime);
            final long genTime=3*60*NANOSEC_PER_SEC;

            while (time<genTime){ //Program runs for 3 minutes
                customerGenerator();

                Random r = new Random();    
                int timeValue=r.nextInt(10);    


                Thread.currentThread().sleep(timeValue * 1000);
            }
        }
        catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }


    public static void customerGenerator(){
        ...code here
        if(selection.equalsIgnoreCase("C")){    
            /**This doesn't working because the customerGenerator is in it's own static class
             * Would the program be more difficult to read if I had everything in the main method?
             * That's what I'm trying to avoid
             * 
             * time=genTime;
               JOptionPane.showMessageDialog(null,"The restaurant is no longer accepting any customers.");
             */

            stop(); //This isn't working because it created a different timer

        }

    }
    public static void stop(){
        final long NANOSEC_PER_SEC = 1000l*1000*1000; 
        long startTime = System.nanoTime();             
        long time = (System.nanoTime()-startTime);
        final long genTime=3*60*NANOSEC_PER_SEC;
        time=genTime;
        JOptionPane.showMessageDialog(null,"The restaurant is no longer accepting any customers.");
    }

}
import java.util.Random;
导入java.util.Scanner;
导入javax.swing.JOptionPane;
公共类内联客户{
@抑制警告(“静态访问”)
公共静态void main(字符串参数[]){
试一试{
最终长纳米秒每秒=1000l*1000*1000;
long startTime=System.nanoTime();
长时间=(System.nanoTime()-startTime);
最终长时间=3*60*纳秒/秒;

虽然(time这显示了与线程通信的几种方式,但最相关的可能是使用
java.util.concurrent.atomic
变量将数据传递给线程

public static void main(String[] args) {

    final java.util.concurrent.atomic.AtomicLong timer = new java.util.concurrent.atomic.AtomicLong((long)(Math.random() * 1000));

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                long timeValue = timer.get();
                do {
                    System.out.println("Customer does something");
                    Thread.currentThread().sleep(timeValue);
                    timeValue = timer.get();
                } while (0 != timeValue);
            }
            catch(InterruptedException interrupt) {
                // whatever;
            }
            System.out.println("Parent says we're all done");
        }
    });
    thread.start();

    try {
        Thread.currentThread().sleep((long)(Math.random() * 10 * 1000));
        boolean do_it_the_easy_way = true;
        if (do_it_the_easy_way) {
            thread.interrupt();
        } else {
            timer.set(0);
        }
    }
    catch(InterruptedException interrupt) {
        System.out.println("Program halted externally");
        return;
    }
}

你没有在任何地方创建线程,你只是在使用主线程。没有“主类”或“静态类”这里,只有一个静态主函数和一个静态customerGenerator函数,这两个函数都属于同一个类。看起来您缺少的不仅仅是一些基本的线程概念。谢谢,但这根本没有回答我的问题。不过,我会编辑这个问题。这是创建线程的一种方法。通常,您不会使用匿名inner类,用于任何非平凡的东西(如客户逻辑)