i need to print some data in a file using 2 threads that run alternative, one for odd index, and the other for even index. The data i have is stored in this array, cursuri = new ArrayList(), that is created from
String nume;
String descriere;
Profesor profu;
Set <Student> studenti;
int[] note;
The data i need is saved in studenti, and this is what i tried so far:
public String[] studentiEven(ArrayList<Curs> c)
{
synchronized (this)
{
for(int i=0;i<c.size();i++)
{
String[] x= new String[3];
int nr=0;
for(Student s:c.get(i).studenti)
{
while(nr%2==1)
{
try {
wait();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
nr++;
notify();
x[0]=s.nume;
x[1]=s.prenume;
x[2]=s.grupa;
return x;
}
}
String[] y=new String[3];
return y;
}
}
public String[] studentiOdd(ArrayList<Curs> c)
{
synchronized (this)
{
for(int i=0;i<c.size();i++)
{
String[] x= new String[3];
int nr=0;
for(Student s:c.get(i).studenti)
{
while(nr%2==0)
{
try {
wait();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
nr++;
notify();
x[0]=s.nume;
x[1]=s.prenume;
x[2]=s.grupa;
return x;
}
}
String[] y=new String[3];
return y;
}
}
Those being 2 functions, and in the main i tried to write:
try {
FileWriter myWriter = new FileWriter("studenti555.txt");
final String[] a = new String[3];
Thread t1=new Thread(new Runnable() {
public void run()
{
a[0]=c.studentiEven(c.cursuri)[0];
a[1]=c.studentiEven(c.cursuri)[1];
a[2]=c.studentiEven(c.cursuri)[2];
try {
myWriter.write(a[0]);
myWriter.write(',');
myWriter.write(a[1]);
myWriter.write(' ');
myWriter.write(a[2]);
myWriter.write('
');
}catch(Exception e)
{
System.out.println("eroare");
}
}
}
);
Thread t2=new Thread(new Runnable() {
public void run()
{
a[0]=c.studentiOdd(c.cursuri)[0];
a[1]=c.studentiOdd(c.cursuri)[1];
a[2]=c.studentiOdd(c.cursuri)[2];
try {
myWriter.write(a[0]);
myWriter.write(',');
myWriter.write(a[1]);
myWriter.write(' ');
myWriter.write(a[2]);
myWriter.write('
');
}catch(Exception e)
{
System.out.println("eroare");
}
}
}
);
t1.start();
t2.start();
myWriter.close();
}catch(Exception e)
{
e.printStackTrace();
System.out.print("teapa frate
");
}
I tried to debug it, but it didn't go through the catch exception or anything, could you guys please help me out?