-1

I have this program in Java (for audio communication with another host) that starts with an instantiation of 2 objects (that are 2 extension of Thread class). In a certain time i need to reboot the program: How can i stop this threads and reboot it? The method Thread.stop is deprecated.

public static void main(String[] args){

            Receiver rx = new Receiver();
            Transmitter tx =new Transmitter();
            rx.start();
            tx.start();
}
narraccino
  • 43
  • 9
  • interrupt is the way to go and set that flag to true is one way. Poison pill/volatile is another for busy spin. You just need to google them. – SMA Oct 16 '17 at 18:14
  • You should not be doing this. Thread.stop is deprecated for a reason. You shouldn't even be dealing with low level classes like Thread. Look at the java.util.concurrency package. – duffymo Oct 16 '17 at 18:24
  • Are your audio transmissions using blocking I/O? – Nathan Hughes Oct 16 '17 at 19:01
  • yes @NathanHughes and i have while loop inside of these extensions threads. – narraccino Oct 16 '17 at 21:14

1 Answers1

-2

The snippet below is a starting point to implement what you need.

public class MyThread implements Runnable, Comparable<MyThread> {

    private static volatile int idCount;

    private volatile int id;
    private volatile boolean running;

    public MyThread() {
        id = idCount++;
    }

    @Override
    public void run() {

        running = true;

        while ( running ) {
            System.out.printf( "Thread %d is running!\n", id );
            try {
                Thread.sleep( 1000 );
            } catch ( InterruptedException exc ) {
                exc.printStackTrace();
            }
        }
    }

    public void stop() {
        this.running = false;
    }

    public boolean isRunning() {
        return running;
    }

    public int getId() {
        return id;
    }

    @Override
    public int compareTo( MyThread o ) {
        return this.id - o.id;
    }
}

public class TestMyThread {

    public static void main( String[] args ) {

        Scanner scan = new Scanner( System.in );
        ExecutorService es = Executors.newCachedThreadPool();
        Map<Integer, MyThread> myThreads = new ConcurrentSkipListMap<>();

        int opt = 0;
        int threadToRemoveId = 0;

        while ( opt != 3 ) {

            System.out.println( "1 - Create and start a thread." );
            System.out.println( "2 - Stops a thread." );
            System.out.println( "3 - Finish all threads." );
            System.out.println( "Choose an option: " );
            opt = scan.nextInt();

            switch ( opt ) {

                case 1:

                    MyThread newThread = new MyThread();
                    myThreads.put( newThread.getId(), newThread );
                    es.execute( newThread );

                    break;

                case 2:

                    System.out.println( "What thread should be removed? " );
                    threadToRemoveId = scan.nextInt();

                    MyThread threadToRemove = myThreads.remove( threadToRemoveId );
                    if ( threadToRemove != null ) {
                        threadToRemove.stop();
                    }

                    break;

                case 3:
                    myThreads.forEach(( Integer t, MyThread u ) -> {
                        u.stop();
                    });
                    es.shutdown();
                    break;

                default:
                    System.err.println( "Wrong option!" );
                    break;
            }


        }

    }

}
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
davidbuzatto
  • 9,207
  • 1
  • 43
  • 50
  • Hi David, thank you for your answer, your code works very well. Can i use it with extensions of Thread right? In my case Receiver and Transmitter are extensions. Should I to add a constructor for passing Runnable ? – narraccino Oct 16 '17 at 21:25
  • @narraccino Yes, you can, since the class Thread implements the interface Runnable and when you extend the Thread class, your class will be a Runnable too ;) The state of the Thread will be controled with a instance variable of your Thread as I coded. If my answer solves your question, please consider to mark it as solved. – davidbuzatto Oct 16 '17 at 21:35
  • IT WORKS!! THANK YOU! – narraccino Oct 17 '17 at 13:33
  • 2
    I don't recommend using this approach. You override deprecated java api and use a ```running``` flag and i bet you don't know what ```volatile``` does. If you would like to do it "the right way"™, please read into ```notify()```, ```wait()``` and interrupting: https://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html – bratkartoffel Jun 11 '18 at 12:59