• Post Reply Bookmark Topic Watch Topic
  • New Topic

web and multithreading

 
rawia karoui
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Part of my app is to provide an interface to an operator to manage multiple printers of smart cards.
A JSP page displays all printers and each printer has a print button.
I want the operator to press several buttons and each button pressed starts a thread.
Threads should run in parallel and pressing a button does not cancel the processing of the previous button.
Do you have any suggestions to solve this problem and thank you in advance
 
Devaka Cooray
Marshal
Posts: 5158
481
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Coderanch, Rawia.

I'm not sure what the 'problem' that you want to solve, but here are some remakes based on your model:

  • Don't always spawn threads on each button click, because spawning a new thread is an expensive task that suchs up resources. Use a thread pool, so you can reuse threads.
  • If you happen to change your model and use thread pools, make sure you have analyzed what type of thread pool does the best for your needs.
  • Make sure your model has a clear addressing on what it would be when 100 users simultaneously click the same button. Do you really want to start 100 threads , or let them go in a queue?

  •  
    rawia karoui
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thank you for this response
    it is just one operator in charge threads number equal to printers number
    after pressing button the launched thread should extract data from a shared queue (between all printers) and send it to the printers
    all printers should work in parallel. I hope this clarify the issue.
    I have no idea about thread pool if this can help me , please can you more explain
     
    Ulf Dittmer
    Rancher
    Posts: 42970
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You may want to familiarize yourself with this part of the Oracle Java Tutorial: http://docs.oracle.com/javase/tutorial/essential/concurrency/executors.html (and bookmark the tutorial as such, if you haven't done so yet).

    Don't always spawn threads on each button click, because spawning a new thread is an expensive task that suchs up resources. Use a thread pool, so you can reuse threads.

    While there were times when thread pools were all the rage due to object creation overhead, I suspect that for threads started one by one manually the overhead isn't even measurable (as opposed to the situation of, say, a web server that might create dozens or hundreds each second). What's more, the Java 6 JVM has reduced that overhead significantly. So I wouldn't worry too much about that.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65520
    105
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't see the need for threads in the first place. If each button click creates a request to the server, the servlet container will take care of running each request in its own thread.

    Are you over-complicating the issue?
     
    rawia karoui
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:I don't see the need for treads in the first place. If each button click creates a request to the server, the servlet container will take care of running each request in its own thread.

    Are you over-complicating the issue?

    the problem that more than one button can be pressed to use the coressponding printers in parallel, that's why i think to launch thread for each submit button
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65520
    105
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Still not seeing it. I assume you are using Ajax to initiate the requests upon a button click? If not, then the page will refresh on each button click, and I don;t see how it could work even if you over-complicate it with needless threading.
     
    rawia karoui
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry if it wasn't so clear.
    for example the operator press the button "print" corresponding to printer1 so a thread is launched, and while this thread is running he press button"print" for printer2 , so an another thread is launched. and the two thread run in parallel
    I hope this is much clear.
     
    Ulf Dittmer
    Rancher
    Posts: 42970
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So the print process runs longer than you want to wait before returning a response to the browser; it make sense to start a thread in that case.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65520
    105
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I still disagree -- that's making it all over-complicated.

    Simply issue an Ajax request on each button press and the servlet container handles each in a thread. The parent page can be notified when the responses come back.
     
    Devaka Cooray
    Marshal
    Posts: 5158
    481
    Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:I still disagree.... Simply issue an Ajax request on each button press and the servlet container handles each in a thread.

    Not sure if I'm correct on this, but doesn't doing that eat up the number threads available to serve requests, in case if the intended operation takes too much time to complete?
     
    Ulf Dittmer
    Rancher
    Posts: 42970
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    doesn't doing that eat up the number threads available to serve requests, in case if the intended operation takes too much time to complete?

    It would eventually, but Rawia said this would be done by a single user one at a time - so the job wold have to take a very long time to finish to exhaust the typical thread pool of a server. Maybe Rawia can chime in on what the expected numbers are; otherwise we're just guessing.
     
    rawia karoui
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:I still disagree -- that's making it all over-complicated.

    Simply issue an Ajax request on each button press and the servlet container handles each in a thread. The parent page can be notified when the responses come back.


    Can you please explain more because i have no idea about ajax
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65520
    105
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you are working on the web and don;t know AJax, that is a technology you need to get under your belt asap!

    There are whole books on Ajax, so I can;t re-write one of them here, but it's a way to make requests back tot he server without submitting the whole page, and it's best to use jQuery to make the requests.

    Search the web for info -- there's plenty.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!