V
V
Vergil17182021-09-01 13:48:43
Java
Vergil1718, 2021-09-01 13:48:43

How to run a thread only once?

I am writing a project in java using spring, on the main page I run a script written in Python. The question arises how to make it run this script 1 time so that it cannot be run again from another computer?
while sketching I am doing a class-thread: (ThreadsClass.java)

package com.blrmyfc.ComputerVision;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ThreadsClass extends Thread {

    private Process process = null;

    public void starting(String command, String script){
        ProcessBuilder processBuilder = new ProcessBuilder(command, script);
        processBuilder.redirectErrorStream(true);

        try {
            process = processBuilder.start();

        } catch (IOException e) {
            e.printStackTrace();
        }

        InputStream stdout = process.getInputStream();
        InputStreamReader isrStdout = new InputStreamReader(stdout);
        BufferedReader brStdout = new BufferedReader(isrStdout);

        String line = null;
        while (true){
            try {
                if (!((line = brStdout.readLine()) != null)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(line);
        }
        int exitVal = 0;
        try {
            exitVal = process.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("выход = "+exitVal);
    }

    public void exit(){
        process.destroyForcibly();
    }

    public void run(){
        String command = "python3";
        String script = "core/blr_recognizer/production.py";
        starting(command, script);
    }
}

Controller class (MethodsController.java)
private ThreadsClass threadsClass;

    public ThreadsClass getThreadsClass(){
        return threadsClass;
    }

    public void setThreadsClass(ThreadsClass threadsClass){
        this.threadsClass = threadsClass;
    }
    @PostMapping("/starting")
    public String starting() {
        ThreadsClass mainThread = new ThreadsClass();
        setThreadsClass(mainThread);
        System.out.println(getThreadsClass().getState());
        getThreadsClass().start();
        return "true";
    }
    @PostMapping("/interrupt")
    public String interruptScript(){
        getThreadsClass().exit();
        getThreadsClass().interrupt();
        return "true";
    }
}


612f5915843ee752974166.png

now I can log in from normal mode and from incognito, and run it. Do I need to somehow synchronize the threads?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
Dmitry Roo, 2021-09-01
@Vergil1718

It is necessary to make some kind of flag in the external system: it can be, for example, some kind of lock file or a record in the database (in SQL or NOSQL)

D
Dmitry, 2014-07-27
@uranus235

at the very beginning put
and then scatter over the code

echo 'Метка 1 - '.round( (microtime(true) - TIMESTART), 5).' сек'.PHP_EOL;
.............
echo 'Метка 2 - '.round( (microtime(true) - TIMESTART), 5).' сек'.PHP_EOL;

and when accessing the page, you will get something like:
Label 1 - 0.003 sec (from the start of execution)
Label 2 - 4.531 sec (from the start of execution)
Label 3 - 4.582 sec (from the start of execution),
from which it will become clear that between label 1 and label 2 something was processed for 4.5 seconds.
Narrow your search by shifting labels until you find the problem.

G
Grag, 2014-07-27
@Grag

Look for dumb loops, hosting issues, client bells and whistles issues, external connections that are incorrectly specified.

I
Igor It doesn't matter, 2014-07-28
@HellFir-e

@uranus235 it's still not the sites to blame, I have a turn, your slow loaded in a split second, and fast 4-5 seconds

M
Myateznik, 2014-07-31
@Myateznik

@uranus235 can you provide links?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question