How to pause a thread and run another thread?

Asked

Viewed 102 times

0

Hello, I need to understand how to get a thread to pause its execution and another thread to start running?

I have the following code snippet:

import java.util.concurrent.Semaphore;

public class AB1AB2 {
  public static void main(String[] args) {
    Semaphore semaforo = new Semaphore(1);

    MutableString ms = new MutableString();

    Thread thrd0 = new Thread(new Accumulator(ms, semaforo), "THREAD 0");
    Thread thrd1 = new Thread(new Accumulator(ms, semaforo), "THREAD 1");

    thrd0.start();
    thrd1.start();

    try{
        thrd0.join();
        thrd1.join();
    }catch(InterruptedException ie){}

    System.out.println(ms.getOrdem());
  }
}

This code snippet creates two threads and executes the following classes:

import java.util.concurrent.Semaphore;

public class MutableString {
    private String ordem ="";

    public String getOrdem() {
        return ordem;
    }

    public void setString(){
        Thread teste = Thread.currentThread();
        if(teste.getName() == "THREAD 0"){
            ordem += "A";
            teste.interrupt();
            ordem += "a";
        } 
        Thread teste2 = Thread.currentThread();
        String teste2Name = Thread.currentThread().getName();
        if(teste.getName() == "THREAD 1") {
            ordem += "B";
            teste2.interrupt();
            ordem += "b";
        }
    }
}

class Accumulator implements Runnable{
    private MutableString sharedValue;
    private Semaphore semaforo;
    private int threadAtual;

    public Accumulator(MutableString acc, Semaphore sempahore) {
        this.sharedValue = acc;
        this.semaforo = sempahore;
    }

    @Override
    public void run() {
        try{
            semaforo.acquire();
            sharedValue.setString();
        }catch(InterruptedException e){
            e.printStackTrace();
        }finally{
            semaforo.release();
        }
    }
}

I’m trying to do the following: The thread0 goes and writes "A" in the variable ordem class MutableString And then, I need that after she adds the letter "A", the thread execution stops, and starts running thread1, which will add "B" to the String order, right after Thread0 comes back and writes "a" and right after thread1 comes back and writes "b", ending the strig order with: "Abab", tried to use interrupted, but I was unsuccessful

  • I don’t know if that would be ideal, but you’ve tried using the sleep()?

  • @Soap I tried, I used Sleep for the thread0, but it just sits there waiting, as I do to run thread1, while thread0 is sleeping?

1 answer

0

Well, the possible solution to your problem would be to use Thread’s methods, which are Wait() and notify().

When thread0 writes "A" in the order variable of the Mutablestring class, you put it to sleep with the Wait() method, this will make it sleep. Soon after, thread1 will add "B" to the String order, and at this point, you will call the notify() method to warn the thread0 to wake it up, and then use Wait() on thread1, which will put it to sleep. And so on and so forth.

I don’t know if you can understand the reasoning for your problem. But that’s the idea.

  • My question is this, how do I run Thread1, then call Wait on Thread0? 'Cause I tried and the show just stood there waiting for Wait to finish and then back to thread0 and then thread1

Browser other questions tagged

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