Reg: Counter Variable in Thread
<p>I have a requirement that should assign a counter variable for each thread that gets invoked. But I am not getting the expected results, actually the counter is duplicating in the threads. I created a dummy table and a proc to insert the counter value into the table. Is there anyway that the code can be changed so that the thread gets an incremented value.</p>

<p>In the below code, the variable <code>counter</code> is a <code>static int</code> </p>

<pre><code>public synchronized int testSequence(){
 System.out.println("testSequence::::: "+counter++);
 //Random rn = new Random();
 CallableStatement cstmt;
 {
  try {
   cstmt = conn.prepareCall("{call insertjtest(?)}");
   cstmt.setInt(1,counter);
   //cstmt.setInt(1, rn.nextInt());
   cstmt.execute();
   cstmt.close();
   conn.commit();
   return counter;
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   return 0;
  }
 }
}
</code></pre>

<p>But I find the </p>
