T
T
thatmaniscool2017-04-30 21:04:19
Java
thatmaniscool, 2017-04-30 21:04:19

How to combine an interface method with a thread in Java?

First of all, I create a class for the message:

public class ArrayIsFull extends Exception{
  private int size;
  
  public ArrayIsFull(int UserSize) {
    size = UserSize;
  }
  
  public String Message (){
    return "Your array is full! Max size is: " + size;
  }
}

Then I create an interface and combine it with the previous one
public interface MyInterface <T> {
   void put (T UserValue) throws ArrayIsFull;
}

Next, I create a working class for the main program, and here I have difficulties:
public class WorkerClass <T> implements Runnable, MyInterface<T> {
  T [] array; // Массив
  Thread flows; // Поток
  private int size;
  
  public WorkerClass(T [] UserArray) {
    size = 0;
    array = UserArray;
    flows = new Thread (this);
    flows.start();
  }

  // И все, тут я застрял, что делать то?:)
  public void run() {
    // TODO Auto-generated method stub

  }

  // Этот метод мне нужно поместить в поток.
  public void put(T UserValue) throws ArrayIsFull {
    if (size == array.length){
      throw new ArrayIsFull (array.length);
    }
    array[size++] = UserValue;
  }
}

And, accordingly, the class with the entry point to the program:
public class MainClass {

  public static void main(String[] args) {
    
    WorkerClass workerclass = new WorkerClass (/*object*/);
    
    try{
      workerclass.flows.join();
    } catch (InterruptedException message){
      System.out.println(message.getMessage());
    }
  }
}

I will be grateful for your help :)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question