• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Thread, update twice

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know why my code always update at least twice into database at the first running but after that it will update once. I guess the problem is from synchronized..Can someone help me? Thank you.
public class sample
extends javax.servlet.http.HttpServlet
implements Runnable
{
private static Object obj = new Object();
public sample()
{
}

public void init()
throws javax.servlet.ServletException
{
new Thread(new sample()).start();
}
public synchronized void run()
{
while (true)
{
try
{
findPath();
Thread.sleep(1000);
}
catch (Exception e) { System.out.println(e);}
}
}
private void findPath()
throws Exception
{
try
{
if(validateComplete())
{
..... //call other methods
saveContentDB();
}
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
private boolean validateComplete()
throws Exception
{
boolean success = false;

......

return success;
}

private void saveContentDB()
throws Exception
{
}
}
[ October 23, 2002: Message edited by: Steffy Sing ]
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic