Keeping the context of a List out of Thread

Asked

Viewed 63 times

0

I wanted to upload the list to Thread and then have access to it after the action is over.

You have this code but at the end of the thread the list is null;

someone would have a tip to help me?

public class ThreadListarClientes {
    private List<Cliente> clientes = null;
    ClienteCTR clienteCTR = new ClienteCTR();
    private Integer contaid = null;

    public ThreadListarClientes carregarThreadListarClientes() throws InterruptedException {

        Thread thread = new Thread() {  

            @Override
            public void run() {      
                clientes = clienteCTR.listar(contaid, "clienteid", false);  
            }
        };
        thread.start();

        while (thread.getState() != Thread.State.TERMINATED) {
            Thread.sleep(100);
        }

        return this;
    }

    public List<Cliente> getClientes() {
        return clientes;
    }

    public void setClientes(List<Cliente> clientes) {
        this.clientes = clientes;
    }

    public Integer getContaid() {
        return contaid;
    }

    public void setContaid(Integer contaid) {
        this.contaid = contaid;
    }
}
  • What language? Java?

  • yes... but I actually think my problem is that Hibernate won’t accept being called inside Thead

1 answer

1

You can use other structures that will help you control the competitor list:

  1. Copyonwritearraylist (javadoc)(example): this solution makes a a copy of the items to be written. In this way, read operations access a different copy. Indicated when you have some updates or inserts in the structure and many readings. We can say that the main difference for an Arraylist is that this structure is thread-safe.

  2. Collections.synchronizedList(list): this other way of controlling blocks the entire list because it uses the same copy for reading and writing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.