a program that terminates threads on a time schedule

Asked 1 years ago, Updated 1 years ago, 34 views

I can't control it.What's wrong?

package com.company;


import java.io.*;
import java.util.*;
import java.util.current.TimeUnit;


public class Main {
    static boolean b=true;
    static ThreadstdRun = null;
    public static void main(String[]args) {

        Runnable inputStreamThread=new Runnable(){
            public void run() {

                while(b){
                    try{
                        // System.out.println("ThreadstdRun start");
                        TimerTask task = new TimerTask() {
                            public void run() {
                                b = false;
                            }
                        };
                        Timer timer = new java.util.Timer();
                        timer.schedule(task, TimeUnit.SECONDS.toMillis(1));
                        while(true){
                            System.out.println("Test";
                            Thread.sleep (200);
                        }
                    } catch(Exceptione){
                        e.printStackTrace();
                    }
                    b = false;
                }

            }

        };

        stdRun = new Thread (inputStreamThread);
            /* Start the thread.*/
        stdRun.start();

            /* Wait for thread to exit*/
        try{
            stdRun.join();
        } catch(InterruptedExceptione) {
            e.printStackTrace();
        }

        System.out.println("Thread Successfully Completed";

    }
}

java

2022-09-30 21:13

2 Answers

I would appreciate it if you could clarify what you want to do more, but is this what you mean?

import java.io.*;
import java.util.*;
import java.util.current.TimeUnit;

public class test {
    static boolean b=true;
    static ThreadstdRun = null;
    public static void main(String[]args) {

        Runnable inputStreamThread=new Runnable(){
            public void run() {

                try{
                    // System.out.println("ThreadstdRun start");
                    TimerTask task = new TimerTask() {
                        public void run() {
                            System.out.println("run");
                            stdRun.interrupt();
                        }
                    };
                    Timer timer = new java.util.Timer();
                    timer.schedule(task, TimeUnit.SECONDS.toMillis(1));

                    while(true){
                        System.out.println("Test";
                        Thread.sleep (200);
                    }
                } catch(Exceptione){
                    e.printStackTrace();
                }
            }

        };

        stdRun = new Thread (inputStreamThread);
            /* Start the thread.*/
        stdRun.start();

            /* Wait for thread to exit*/
        try{
            stdRun.join();
        } catch(InterruptedExceptione) {
            e.printStackTrace();
        }

        System.out.println("Thread Successfully Completed";

    }
}


2022-09-30 21:13

Hmm... Then it will loop indefinitely.

There is an infinite loop in while after timer.schedule(), so
When the timer starts, while (b) will not run for life.

Unless you're forcing a thread to quit (kill) in a timer task,
This time, I'm just changing the flag, so I'm going to look at the flag at a certain time, that is,

where(true) under timer.schedule() is while(b) should be

In that case, the first line, while (b) I don't really understand the meaning of , but I guess there's something wrong with it


2022-09-30 21:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.