Answer the question
In order to leave comments, you need to log in
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;
}
}
public interface MyInterface <T> {
void put (T UserValue) throws ArrayIsFull;
}
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;
}
}
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());
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question