Thread program clarified
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Raja_Sarkar
Posted On:   Wednesday, December 18, 2002 04:41 PM

Hi I am developing a logon screen that a servlet will invoke upon initialization. I wish to clarify with the community if my thoughts are on track or if there could be a better way of handling it. Following is the sequence of program flow: The logOn screen is developed using swing. The screen generation and the button listners are stored away in a java bean (calling it - LogOnScreen). The LogOnScreen is made to run on a thread and hence implements Runnable. class LogOnScreen implements Runnable{ . private boolean status = false; . . private void setStatus(...){ ... } public boolean getStatus(){    More>>

Hi


I am developing a logon screen that a servlet will invoke upon initialization. I wish to clarify with the community if my thoughts are on track or if there could be a better way of handling it.


Following is the sequence of program flow:


The logOn screen is developed using swing. The screen generation and the button listners are stored away in a java bean (calling it - LogOnScreen). The LogOnScreen is made to run on a thread and hence implements Runnable.


			
class LogOnScreen implements Runnable{
.
private boolean status = false;
.
.

private void setStatus(...){
...
}

public boolean getStatus(){
...
}
//Button action
JButton loginButton = new JButton("Login");
loginButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
setUserId(userTextField.getText());
setPassword(new String(passwordField.getPassword()));
//Calls service for authentication.
boolean isSuccess = DmUtil.authenticateUser(getUserId(), getPassword());
if(isSuccess){
setStatus((byte)'S');
}
else{
setStatus((byte)'F');
}
}
}


Servlet has the LogOnScreen object as its member field.


			
public class demo_WS extends HttpServlet{
LogOnScreen lg;
..
..
}


Browser calls the servlet. The servlet calls the LogOnScreen and starts the thread. It then goes into a loop and checks the status of a flag and breaks out of the loop only if the flag is true. After breaking out it directs the service to another JSP/Servlet.


			
while(lg.getStatus() != (byte)'S' && lg.getStatus() != (byte)'C'){
Thread.sleep(1000);
//If action cancelled
if(lg.getStatus() == (byte)'C')
break;
}
// If success then requestDispatcher forwards the request to another jsp




This is the basic idea.


Since time-slicing happens with Windows, the 'main' thread and the LogOnScreen thread shares CPU simulteneously without problem.


Is there a better approach specially if we dont consider time-slicing being done by the OS ?


Looking forward to your comments.
-
Raja

   <<Less

Re: Thread program clarified

Posted By:   Muruganantham_Mani  
Posted On:   Thursday, December 26, 2002 09:45 PM

Raja,

You could have implement the thread notification mechanism to avoid the unnecessary looping of LogonScreen status.



The code looks like this


synchronized (this)
{
wait();
}

after invoking the LogonScreen

In the LogonScreen, after everything is loaded, then

synchronized(.class)
{
notify();
}

This could avoid the unnecessary looping in the servlet.





Regards

Muruga
About | Sitemap | Contact