public class Main {
final static ArrayList<Integer> list= new ArrayList<>();
public static void main(String[] args) {
Thread writer = new Thread(Main::write);
Thread reader = new Thread(Main::read);
writer.start();
reader.start();
}
public static void write(){
while (true){
list.add(1);
}
}
public static void read(){
int currentIndex=0;
while (true){
if(currentIndex>=list.size()){
try {
Thread.sleep(1);
continue;
} catch (InterruptedException e) {
return;
}
}
int element = list.get(currentIndex);
currentIndex++;
//do stuff with element...
}
}
}
public class Main {
final static ArrayList<Integer> list= new ArrayList<>();
public static void main(String[] args) {
Thread writer = new Thread(Main::write);
Thread reader = new Thread(Main::read);
writer.start();
reader.start();
}
public static void write(){
while (true){
list.add(1);
}
}
public static void read(){
int currentIndex=0;
while (true){
if(currentIndex>=list.size()){
try {
Thread.sleep(1);
continue;
} catch (InterruptedException e) {
return;
}
}
int element = list.get(currentIndex);
currentIndex++;
//do stuff with element...
}
}
}