Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Valores nulos

Minha implementação está imprimindo valores nulos para a maioria dos valores na lista...

Segue output (está em inglês):

Going to sleep, waiting notification...
List is full, notifying...
0 - Thread 0 - 0
1 - Thread 0 - 1
2 - Thread 0 - 2
3 - Thread 0 - 3
4 - Thread 9 - 0
5 - Thread 9 - 1
6 - Thread 9 - 2
7 - Thread 9 - 3
8 - Thread 9 - 4
9 - Thread 9 - 5
10 - Thread 9 - 6
11 - Thread 8 - 0
12 - Thread 8 - 1
13 - Thread 7 - 0
14 - Thread 7 - 1
15 - Thread 7 - 2
16 - Thread 7 - 3
17 - Thread 7 - 4
18 - Thread 7 - 5
19 - Thread 7 - 6
20 - Thread 7 - 7
21 - Thread 7 - 8
22 - Thread 7 - 9
23 - Thread 7 - 10
24 - Thread 7 - 11
25 - Thread 6 - 0
26 - Thread 5 - 0
27 - Thread 5 - 1
28 - Thread 5 - 2
29 - Thread 4 - 0
30 - Thread 4 - 1
31 - Thread 4 - 2
32 - Thread 4 - 3
33 - Thread 3 - 0
34 - Thread 3 - 1
35 - Thread 3 - 2
36 - Thread 3 - 3
37 - Thread 2 - 0
38 - Thread 2 - 1
39 - Thread 2 - 2
40 - Thread 2 - 3
41 - Thread 1 - 0
42 - Thread 2 - 4
43 - Thread 2 - 5
44 - Thread 2 - 6
45 - Thread 3 - 4
46 - Thread 3 - 5
47 - Thread 4 - 4
48 - Thread 4 - 5
49 - Thread 5 - 3
50 - Thread 6 - 1
51 - Thread 6 - 2
52 - Thread 6 - 3
53 - Thread 6 - 4
54 - Thread 6 - 5
55 - Thread 7 - 12
56 - Thread 8 - 2
57 - Thread 8 - 3
58 - Thread 8 - 4
59 - Thread 8 - 5
60 - Thread 8 - 6
61 - null
62 - null
63 - null
64 - null
65 - null
66 - null
67 - null
68 - null
69 - null
70 - null
// Todo o restante está como null
2 respostas

Minhas classes (todas estão em inglês):

package br.com.alura.threads.mylist;

public class MyListMain {

    public static void main(String[] args) throws InterruptedException {
        final MyList myList = new MyList();

        for (int i = 0; i < 10; i++) {
            new Thread(new AddElementTask(myList, i)).start();
        }

        new Thread(new PrintTask(myList)).start();
    }

}
package br.com.alura.threads.mylist;

public class MyList {

    private final String[] elements = new String[1000];
    private int index = 0;

    public int size() {
        return this.elements.length;
    }

    public synchronized void addElement(final String element) {
        this.elements[index] = element;
        this.index++;

        try {
            Thread.sleep(10);
        } catch (final InterruptedException e) {
            e.printStackTrace();
        }

        if (index == element.length()) {
            System.out.println("List is full, notifying...");
            this.notify();
        }
    }

    public String getElement(int index) {
        return this.elements[index];
    }

}
package br.com.alura.threads.mylist;

public class AddElementTask implements Runnable {

    private final MyList list;
    private final int threadNumber;

    public AddElementTask(final MyList list, final int threadNumber) {
        this.list = list;
        this.threadNumber = threadNumber;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            list.addElement("Thread " + threadNumber + " - " + i);
        }
    }

}
package br.com.alura.threads.mylist;

public class PrintTask implements Runnable {

    private final MyList myList;

    public PrintTask(final MyList myList) {
        this.myList = myList;
    }

    @Override
    public void run() {
        synchronized (myList) {
            try {
                System.out.println("Going to sleep, waiting notification...");
                myList.wait();
            } catch (final InterruptedException e) {
                e.printStackTrace();
            }

            for (int i = 0; i < myList.size(); i++) {
                System.out.println(i + " - " + myList.getElement(i));
            }
        }
    }

}
solução!

Já encontrei o erro na *MyList.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software