Running code snippets in parallel

Asked

Viewed 321 times

0

Hello, I wanted to know how to run one thread after another for a certain time, for example:

Thread x = new Thread(() -> {
// qualquer código, pode ter até um while(true)...
});
Thread x2 = new Thread(() -> {
// qualquer código, pode ter até um while(true)...
});

I have a thread x that should run for 3s, after which it should pause and then x2 should start, but it cannot contain any additional code within its runnable. Thank you in advance.

  • 1

    Do you want to use threads for synchronous tasks? Why?

  • Because I have to execute several independent codes in a certain period of time, like I want x seconds to run a code snippet, but that goes back to where it left off. I don’t know if I made it clear :/

  • @V.Azzone I couldn’t understand it very well. I suggest you edit your question and provide more details of what you want to do.

1 answer

-1

Dear colleague,

The best thing is to create a class that inherits Thread and insert the run(){} method. This method you can put a Synchronized in the method, this way, when you can call the start method and to pause just call the Sleep(time) method, passing in the parameter the time you need it to wait.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.