Hi,
I am sorry for bothering you with the same topic :-) but I guess perhaps
now I know the answer for the two questions in my last email:
0
I guess if I don't use "single Thread Model", normally,
[J2SE1.3(windows) + Jsdk2.1] will work as the following way:
[a] for every "custom Servlet class" , ONLY make one instance for it.
[b] for every "client accessing" to this "custom Servlet class",
Servlet engine will make a NEW Thread with that ONLY one
instance mentioned in [a] -- not making another instance.
[c] so I guess we can "lock" our code because of [a] and [b].
1
BTW , I correct a little for my Servlet class -- I only put "while..." in
"synchronized(sync){...}", so now if I start my first Application and
second Application "in turn", the two Applications will all
receive the replying from Servlet engine , and then they all "end" --
it means that two new Threads have been made, and they are
working ; but before all the 10 "11111" have been already printed,
the 10 "22222" will not be printed -- this Thread is being blocked
now . The following is the "updated" code of my Servlet class:
**************************************************************
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class servlet_ht extends HttpServlet {
ObjectOutputStream os=null;
ObjectInputStream is=null;
String s_tmp=null;
//private static Object sync=new Object();
Object sync=new Object();
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException{
int i=0;
System.out.println("get a GET..., P1="+request.getParameter("P1"));
try{
response.setContentType("application/octet-stream");
os=new ObjectOutputStream( response.getOutputStream() );
os.writeObject("in GET=aaaaa");
os.flush();
/*
is=new ObjectInputStream( request.getInputStream() );
s_tmp=(String)is.readObject();
System.out.println("in doGet, return from applet ="+s_tmp);
*/
//is.close(); is=null;
os.close(); os=null;
synchronized(sync){
while(i<10){
System.out.println("P1="+request.getParameter("P1")+"
thread="+Thread.currentThread()+" sync="+sync);
i++;
Thread.currentThread().sleep(1000);
}
sync.notifyAll();
}
}catch(Exception e){System.out.println("e="+e);}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException{
System.out.println("get a POST...");
try{
is=new ObjectInputStream( request.getInputStream() );
s_tmp=(String)is.readObject();
response.setContentType("application/octet-stream");
os=new ObjectOutputStream( response.getOutputStream() );
os.writeObject("in POST="+s_tmp);
os.flush();
is.close(); is=null;
os.close(); os=null;
}catch(Exception e){System.out.println("e="+e);}
}
public String getServletInfo(){
return "servlet_ht";
}
}//class
**************************************************************
Have a nice weekend! :-)
Bo
Oct.06, 2000
Bo Xu wrote:
> Hi,
>
> I just want to add a note to the testing in my last email, because I can
> not explain [0] and [1] by myself :-)
>
> [0]
> If I use both the following:
> Object sync=new Object();
> or
> private static Object sync=new Object();
>
> I get the same result! :-) and when I:
> System.out.println("P1="+request.getParameter("P1")+"
> thread="+Thread.currentThread()+" sync="+sync);
>
> I always get the the same sync -- "java.lang.Object@697676" :-) (even if I
> restart my Servelt engine). I guess now my Servelt engine only make
> ONE instance of my class -- servlet_ht. But if so, how to explain [1] ?
>
> [1] If I don't do the following:
> synchronized(sync){
> ...
> sync.notifyAll();
> }
>
> I will get the "cross" of "11111" and "22222" in the
> "DOS Command Prompt" of my Servlet engine
>
> Could anybody explain [0] and [1] ? Thanks in advance!
>
> 100 cent Receiver -- Bo :-)
> Oct.06, 2000
>
___________________________________________________________________________
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