• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is a Thread and in what situations I should use Threads.

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody, I am Krishna, I have a doubt regarding threads, I executed a simple program where I created two threads and I will switch among these threads and print something to the console. But I haven't understood where to use threads in real time applications. Can anyone give me an example scenario.

Thanks to all in advance, and forgive me if my question is wrong.

Have a nice day.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To the Java™ tutorials! See whether that helps.
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:To the Java™ tutorials! See whether that helps.



Hi Campbell, I am at present reading that tutorial only, I am unable to understand that, they are mentioning how to use threads, but not where and in what scenarion to use threads. Can you please give me a simple example if you don't mind.

Thank you in advance.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anywhere where two people need simultaneous access to the same data will require concurrency.
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody, I was concentrating on the basics of java, so I left this topic, now I think I know some basics of java, don't know how far I am true.

I was seeing my friend coding for a serial port, the aim is, there is an Radio Frequency Identification kit connected to the serial port. There are some Swipe cards in which some code is embedded. When ever the card is swiped the kit reads the code present in the card and sends it to the serial port.

The java program running on that machine listens an interrupt, then it reads the data on the serial port and displays the code. This was his project to track employees attendance.

Till now it is clear to me, when I have seen the code he was implementing Runnable. Why do he need to create a Thread there.

I copied the same code, removed the Runnable interface and entire Thread related code and executed the code. I haven't seen any difference.

Why should I use a Thread there. Can anybody please tell me.

Thank you all in advance. Have a nice day.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well Krishna ..Your question is genuine to know why your friend use thread and runnable interface in the application.why not code without thread ? Suppose a multiple request arrives when you swipe the card at single point of time. it may be more than one person swipe the card at same time. which request will first take your code. This type of situation is called as Deadlock. your code will failed in this case. if it is a single user then your code will run for a single request. Multiple request is handel by Thread. Kindly let me know if you have any queries.
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Pratyusendu for the detailed explanation. However I have one more doubt, as Campbell said, Threads can be used where two people need simultaneous access to same data. What does it mean?

Thank you in advance and welcome to coderanch. Nice to see you here.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I smell some slightly out of subject content here I see both Campbell and Pratyusendu talking about concurrency. This does not explain why we use threads. If you think about GUIs, you can understand how useful threads are. Imagine that you have a application, with a window and a few buttons. Each button does something very clever, but also very heavy. What would happen with a single thread ? After clicking a button, you'd have to wait for your very heavy logic to finish. You could not press other buttons until it finishes. On the contrary, if you'd make the heavy logic run in a new thread, you'd be spared from that. You could press other buttons, while the heavy logic would be running behind, in another thread. Which now brings issues mentioned by Campbell and Pratyusendu.


(Pratyusendu, you'll have to check again what a deadlock is )
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Threads can be used where two people need simultaneous access to same data. What does it mean?



Simple Example :-

Air Ticket Booking System/ Train Ticket Booking System

Two or more people can check the tickets availability at the same time.

 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Christophe, I experienced this kind of problem when one of my friend was coding a GUI. I will tell him and I will also simulate this practically and let you know if I am successful.

One thing before continuing, should I implement the thread in GUI or in the logic that was executed when I pressed the button.

I mean if the GUI is in one class and logic is in another class, which class should implement the Thread.

class MyGUI implements Runnable

or

class MyLogic implements Runnable.

Thank you in advance.
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Brij Garg, I did not understand what you said.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your logic should be Runnable, not your GUI. If you are using Swing, everything related to the GUI should be run on a special thread called the Event Dispatch Thread. There's already some API to help you achieve this (SwingUtilities#invokeLater). The heavy logic should run in its own thread though, and only updating the GUI should be done in the Event Dispatch Thread. There's a class called SwingWorker in Java6, which helps you manage that. This is becoming a bit advanced though, so you should try more simple examples first !
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Chritstophe.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Here is one of the example where we can use threads.

Take the below sample for loop

for(Object data: objects){
doSomeProcessing(data) -- let this processing of the data takes 10 seconds
}
There are totally 100 objects. In this scenario the total time taken will be 100*10= 1000Seconds

If the "doSomeProcessing" task is made as a separate thread and left for execution then all the threads wil get executed in parallel hence the time taken will be 10s. This will be a considerable improvement in the performance but with more memory.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My thoughts on the RFID stuff. The intend of the application is to read data from the port about an employee and record his attendance somewhere.

Now while your program is recording the attendance(after reading from port is complete) what if another employee swipes his card. Lets say recoding the attendance to a database is a time consuming task. So if threads are not there the program will accept another input only if it has completed recording the attendance of the previous employee.
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Manohar and Rahul for the examples.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't ask unrelated questions on somebody else's thread, krishna anusha. What a shame I have been busy and cannot delete the answers in time, but I can give you a bad mark with the other moderators.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Krishna Anusha has apologised and I accepted the apology ; he has agreed to start a new thread if it is necessary to continue this discussion. I was mistaken about it being somebody else's thread; it was infact his thread in the first place. Sorry.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manohar Puttaswamy wrote:Hi,

Here is one of the example where we can use threads.

Take the below sample for loop

for(Object data: objects){
doSomeProcessing(data) -- let this processing of the data takes 10 seconds
}
There are totally 100 objects. In this scenario the total time taken will be 100*10= 1000Seconds

If the "doSomeProcessing" task is made as a separate thread and left for execution then all the threads wil get executed in parallel hence the time taken will be 10s. This will be a considerable improvement in the performance but with more memory.



I dont think that this is true.. It still takes 1000 seconds for the entire processes to be executed (or may be more considering the context switching overhead for a processor).. the processor gives a fraction of time to process the first thread then second then third and so on.. and then if the first thread is not yet completed it will still be allocated with some more processor time.. and this will be repeated till it is executed completely.. again it depends on the job scdeduling algorithm of the particular processor..
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jiji, I was just thinking the same thing. If it takes 10 seconds of processor time for one object then I can't see how it will make significant difference to the total time taken (1000seconds) whether they are done one after the other or run as 100 threads.

Would it make a difference if there were multiple processors?
 
Jiji Cherian
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can not give an authentic answer becuase I do not know much about how multi-processor works. [] .But if you think about it the answer is YES.
Becuase when the tasks are shared between processors the total time takes will be less.
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, thank you all for your valuable replies. I have been studying about threads, I am unable to implement event dispatching. I have read a lot in the web, but there are no bold examples available. I only understood the concept. But unable to implement it. Moreover I am unable to understand invokeLater() usage.

Lengthy programs are placed, I am unable to understand them. Please can anyone tell me how to implement this one with an example program.

I am working on an application, where I will be sending messages to mobiles from a system using a GSM modem. I want to implement this one in my application.

I have coded the entire thing, but the problem is once I hit the send message button the GUI is becoming inactive, after sending all messages it is becoming active.

Then I understood that I have to use Event Dispatching concept. Can anyone help me with small examples?

Thank you all in advance, have a great day.
 
Doug Braidwood
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krishna,
I'm not sure about that but it might be best to start a new topic for this question, perhaps in the "Threads" forum with an appropriate topic title.
Best regards,
Doug
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Dude.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Doug Braidwood wrote:Hi Krishna,
I'm not sure about that but it might be best to start a new topic for this question, perhaps in the "Threads" forum with an appropriate topic title.
Best regards,
Doug

You are right; thank you for noticing what I missed yesterday.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic