I have a problem to get two threads working on the same array. So far I am not concerned about the synchronisation because I do not get them operating on the same array.
I tried three approaches so far. (I think of the execution of the main function as a thread, so in the code samples I will only create 1 thread).
1) I create an array inside the main function, but when I try to tell the thread inside its run() function to manipulate the array in the main function it says the array I am refering to can not be resolved to a variable.(dont worry about allZero(),noZero(),writeZero(),writeOne(). This code is solely written for this post in order to focus on my real problem: How to get two threads work on the same array?)
public class StackOverflow1 implements Runnable {
public static void main(String[] args) {
int a[]={0,0,0,0,0,0,0,0,0,0};
Thread thread= new Thread(new StackOverflow1());
thread.start();
while(true){
if(!allZero(a)){
writeZero(a);
}
}
}
@Override
public void run() {
while(true){
if(!noZero(a)){
writeOne(a);
}
}
}
}
2) So, I create an array as part of the class that determines the thread. Now the problem is, when I want to manipulate the array inside the main function it says that it can not make static reference to a non static field.
public class StackOverflow2 implements Runnable {
int []a={0,0,0,0,0,0,0,0,0,0};
public static void main(String[] args) {
Thread thread= new Thread(new StackOverflow2());
thread.start();
while(true){
if(!allZero(a)){
writeZero(a);
}
}
}
@Override
public void run() {
while(true){
if(!noZero(a)){
writeOne();
}
}
}
}
3) If I create two instances of a thread which both have an array as part of their class, then I have just two threads both operating on their own array. How can I get two threads operating on the same array? Thank you for your help
Edit: I will describe my current situation a bit more detailed. In about three weeks I have to write an exam about simulation. The exam will not contain a single code line. It will be all about statistics (problems on maximum likelihood estimators and chi^2 tests) and probability theory. But a key model of the lecture are M/M/x queues. I am not a coder. Coding is not my hobby. Therefore I will miss many implications of statements being made in books or here on Stack Overflow.
All I want is to get some intuition for M/M/x queues by using my little coding skill (I am not even using a queue. It is an array of fixed length) in order to prepare my brain to learn the theory that is contained in the lectures and tutorials. Does this make sense? So my problem (the exam) is already split into many sub-problems and all I was asking here is help for one of my nuggets. To be fair the original question hides my situation.