Does anyone know how to stop a Java thread? I am not very familiar with Java and am not really sure exactly what I am doing. I have to create the necessary classes to facilitate an indirect blocking and non-blocking communication environment between threads (producers and consumers) using a bounded buffer whose size is defined at run time. I have to test the working of these classes by creating an environments where 2 producers and 2 consumers communicate with each other by arbitrarily choosing either blocking or non-blocking communication. I have all of the classes defined, i.e., BoundedBuffer, Buffer, Producer, Consumer, Channel (the mailbox), etc. The program will create the threads and run them but I cannot get the threads to sleep. I tried a "producerThread.sleep(1000)" command but it does not like that command. Any suggestions? Thanks a lot.

Here's the code:

// BoundedBuffer class

public class BoundedBuffer implements Buffer 
{
    private static final int BUFFER_SIZE = 5;
    private int count;
    private int in;
    private int out;
    private Object[] buffer;
    private Consumer C;
    private Producer P;

    public BoundedBuffer()
    {
        count = 0;
        in = 0;
        out = 0;
        buffer = new Object[BUFFER_SIZE];
    }

    public void send(Consumer C, Object item)
    {
        while (count == BUFFER_SIZE);

        count++;
        buffer[in] = item;
        in = (in + 1) % BUFFER_SIZE;
    }

    public Object receive(Producer P)
    {
        Object item;
        while (count == 0);
        --count;
        item = buffer[out];
        out = (out + 1) % BUFFER_SIZE;

        return item;
    }
}

// Bounded class

public class Bounded 
{
    public Bounded()
    {
        Buffer mailBox = new BoundedBuffer();
        Channel mailBox1 = new MessageQueue();

        Thread producerThread = new Thread(new Producer(mailBox));
        Thread consumerThread = new Thread(new Consumer(mailBox));
        Thread producerThread2 = new Thread(new Producer2(mailBox1));
        Thread consumerThread2 = new Thread(new Consumer2(mailBox1));

        producerThread.start();
        consumerThread.start();
        producerThread2.start();
        consumerThread2.start();
    }

    public static void main(String args[])
    {
        Bounded server = new Bounded();
    }
}

// Buffer Interface

public interface Buffer 
{
    public abstract void send(Consumer C, Object item);
    public abstract Object receive(Producer P);
}

// Channel Interface

public interface Channel 
{
    public abstract void send(Consumer2 C, Object item);
    public abstract Object receive(Producer2 P);
}

// Producer class

import java.util.Date;

class Producer implements Runnable
{
    private Buffer mbox;
    private Consumer C;

    public Producer(Buffer mbox)
    {
        this.mbox = mbox;
    }

    public void run()
    {
        Date messageA;
        int count = 0;
        while (true)
        {
            messageA = new Date();

            System.out.println("Producer!!!!!!!!!!! produced" + messageA);
            mbox.send(C, messageA);
            count++;
        }
    }
}

// Producer2 class

import java.util.Date;

class Producer2 implements Runnable
{
    private Channel mbox;
    private Consumer2 C;

    public Producer2(Channel mbox)
    {
        this.mbox = mbox;
    }

    public void run()
    {
        Date messageA;
        int count = 0;
        while (true)
        {
            messageA = new Date();

            System.out.println("Producer2 produced" + messageA);
            mbox.send(C, messageA);
            count++;
        }
    }
}


// Consumer class

import java.util.Date;

 class Consumer implements Runnable
 {
    private Buffer mbox;
    private Producer P;

    public Consumer(Buffer mbox)
    {
        this.mbox = mbox;
    }

    public void run()
    {
        Date messageA;

        while (true)
        {
            messageA = (Date)mbox.receive(P);
            if (messageA != null)
                System.out.println("Consumer!!!!!!!!!!!!!! consumed" + messageA);
        }
    }
}


// Consumer2 class

import java.util.Date;

class Consumer2 implements Runnable
{
    private Channel mbox;
    private Producer2 P;

    public Consumer2(Channel mbox)
    {
        this.mbox = mbox;
    }

    public void run()
    {
        Date messageA;

        while (true)
        {
            messageA = (Date)mbox.receive(P);
            if (messageA != null)
                System.out.println("Consumer2 consumed" + messageA);
        }
    }
}

// MessageQueue class

import java.util.Vector;

public class MessageQueue implements Channel
{
    private Vector queue;
    private Producer2 P;
    private Consumer2 C;

    public MessageQueue()
    {
        queue = new Vector();
    }

    public void send(Consumer2 C, Object item)
    {
        queue.addElement(item);
    }

    public Object receive(Producer2 P)
    {
        if (queue.size() == 0)
            return null;
        else
            return queue.remove(0);
    }
}

Hi everyone,

When you see the threads api you will see the stop() function but the thing is that it is not used anymore so i guess when when you start a thread there's no way for you to stop it.

Anyone correct me if i am wrong

Richard West

There is no way to force a thread to stop, and for good reasons as doing so could leave the entire application in an unpredictable state.

The standard means is to have a public function in the Runnable instance used by the thread which sets some flag.
The run method at one or more places (usually as a loop condition for an otherwise eternal loop) checks whether that flag is set and if it is terminates the loop which in turn ends the run method which causes the thread to terminate gracefully.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.