On Thu, 5 Oct 2000, Tiana Zhang wrote:
> Hi,
> 1, My servlet is not in the singleThread mode.
>
> 2, The db.insert(or db.upgrade) are external class which handle the
> database access. it is a separated class, I wrote a test case for it
> and they all working fine.
>
> 3. I isolated assignNum() method from the servlet into a standard
> class. Then I created a test program to generated 50 threads which
> each of them is gonna to call the assingNum() method. At first I
> didn't apply the static to the method. It turned out that all the
> threads return the same number after calling the assignNum(). After
> I added the static, all threads return unique number which is what I
> wanted.
What do you mean by "return the same number"? What number? Where is
a number being returned in your code? Are you talking about rID?
That is a String.
For that matter, why is the method called assignNum() to begin with?
All of the parameters are Strings.
Without knowing more about your DBaccess class, it is hard to say what
the problem really is here.
One thing that might be a factor with static the way you have it here,
is because each time you call "new Number()", it creates a new
instance of that class.
It seems you don't fully understand how synchronized works (and it
ain't necessarily easy). In particular, you synchronize on some
object. If you don't specify an object to synchronize on, "this" is
used. So each of your Number objects synchronizes on itself, which
doesn't do any good if you want to synchronize across Number objects.
Making the method static means that the synchronization is on the
class, so that does work across Number objects.
However, that does not mean that that is a correct or a good
solution. For example, instead of making assignNum() static, you
could do (in Number):
private static Object syncObj = new Object();
and then synchronize on syncObj on the relevant block within
assignNum(), a la:
synchronized (syncObj) {
}
Then assignNum() wouldn't have to be static or synchronized.
Of course, if I understand you correctly, this is not really the way
you are doing things within your servlet, the structure is different,
and you don't have multiple instances of the class that contains the
assignNum() function. (Or maybe you do, it's not really clear.)
> Because of the complexity of the database access code, I didn't post
> the code. Here is the test code to create multiple threads to call
> the assignNum() method:
>
> public class MultipleThreadsTest {
> public static void main (String[] args) {
> for (int i = 0; i < 50; i++){
> new SimpleThread(String.valueOf(i)).start();
> }
> }
> }
>
> public class SimpleThread extends Thread {
> public SimpleThread(String str) {
> super(str);
> }
>
> public void run() {
> for (int i = 0; i < 50; i++) {
> Number numberObj = new Number();
> System.out.println(Number.assignNum("test", "1111", "TT", "cmt none"));
> try {
> sleep((long)(Math.random() * 1000));
> } catch (InterruptedException e) {}
> }
> System.out.println("DONE! " + getName());
> }
>
> }
>
> public class Number{
> public static synchronized String assignNum(String name, String uID,
> String requestType, String
> comment){
> String rID = "";
> //DBaccess is the database access class. It used Oracle SID to make the
> connection and insert, update the table.
> DBaccess db = new DBaccess();
> rID = db.DBinsert(name, uID, requestType, comment);
> db.updateSeqNum(requestType);
> return rID;
> }
> }
>
>
> Thanks for all the replies.
>
> Tiana
>
> On Thu, 5 Oct 2000 11:48:45 -0500, Milt Epstein <[EMAIL PROTECTED]> wrote:
>
> >On Thu, 5 Oct 2000, Tiana Zhang wrote:
> >
> >> The reason I think I have to put static keyword for the method is
> >> because my method is going to update the database by JDBC
> >> connection. I don't want multiple thread updating the database at
> >> the same time.
> >
> >static should have nothing to do with this. static just controls
> >whether something is a class variable/method or an instance
> >variable/method. (If you don't understand the difference, you should
> >get a basic Java book.)
> >
> >
> >> Here is my code:
> >>
> >> public static synchronized String assignNum(String name, String uID, String
> >> requestType, String comment){
> >>
> >> String rID = "";
> >> try{
> >> rID = db.DBinsert(name, uID, requestType, comment);
> >> db.updateSeqNum(requestType);
> >>
> >> } catch (Exception e) {
> >> System.err.println(e.toString());
> >> }
> >> return rID;
> >> }
> >
> >There's lots of information missing here. Like what is db? What do
> >its methods do? How is assignNum called? And do you have your
> >servlet implementing SingleThreadModel? There may be other relevant
> >considerations as well.
> >
>
Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html