1

I am working for multithreaded application, so i have a thread which has one Global ArrayList variable, I am fetching list of values from database and assigning it to that variable. I have restricted how many list should fetch, to 100. In backend i am using hibernate.

I have kept this thread running in background periodically, But what i wanted to do that, whenever this thread runs, it will fetch next 100 list of items. And also i don't want to use static and don't want to maintain it in a database, i am confuse how to maintain a counter that will tell my fetching method that next 100 item to fetch. can anybody help, how can i achieve this? Following is my code sample,

public class DemoThread implements Runnable {

    private ArrayList <<>> samplePojoList = null;

    @Override
    public void run() {
        MyDaoClass dao = null;

        try { 
            // this dao method fetching list of 100 results from database, 
            // for each iteration i want to fetch next 100 result from 
            // database 
            samplePojoList = dao.getSampleList();
        } catch (Exception e) {}
    }
}
Derek
  • 4,575
  • 3
  • 22
  • 36
Tushar Deshpande
  • 448
  • 2
  • 9
  • 28
  • I know you don't want to use static, but why not just make a singleton with a synchronized getInstance method? – inafalcao May 20 '16 at 04:30
  • @inafalcao . NO . Singletons have many problems to begin with, and related synchronization is difficult. Usually, you're far better off adding a parameter to a constructor and passing in something relevant. – Clockwork-Muse May 20 '16 at 05:15
  • Question - your thread finishes, and then you re-submit it, right? It isn't running continuously? You're going to have to pass in a variable at some point to the database to tell where you left off (well, unless you're doing something with cursors, but that's a slightly different problem), so you might as well pass a parameter to the thread too. Oh, side note: databases don't really have a concept of "next" or "previous" per se, you have to tell them if/how you want things ordered... – Clockwork-Muse May 20 '16 at 05:19

3 Answers3

1

You can very well make use of ThreadLocal variable in this case.

Please see example below:

private ThreadLocal<Integer> counter = new ThreadLocal<Integer>();
shankarsh15
  • 1,947
  • 1
  • 11
  • 16
1

Here is a simplified example how you can pass a counter to your threads and maintain a synchronized count among your consumer threads. In order to maintain the counter count consistent among threads, it should be accessed and increased in the synchronized block. My assumption is that you want a counter to be shared among all your consumer threads, not one counter per consumer thread.

class Counter{
    int count = 0; 
}

class MyThread implements Runnable {
    Counter counter; 
    MyThread(Counter counter){
        this.counter = counter;
    }
    public void run() {
        int count;
        synchronized(counter){
            count = counter.count; 
            counter.count++; 
        }

        // fetch 100 times the value of this counter
        System.out.println(count); // just a stub code
    }
}

public class CounterDemo {

    public static void main(String[] args) {
        Counter counter = new Counter(); 
        MyThread t = new MyThread(counter);
        new Thread(t).start();
    }
}
apadana
  • 13,456
  • 15
  • 82
  • 98
0

You can use AtomicReference to achieve the same.

Sample code for String:

String initialReference = "value 1";

AtomicReference<String> someRef =
    new AtomicReference<String>(initialReference);

String newReference = "value 2";
boolean exchanged = someRef.compareAndSet(initialReference, newReference);
System.out.println("exchanged: " + exchanged);

You can replace String with ArrayList in above example.

Have a look at this SE post for more details:

When to use AtomicReference in Java?

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211