wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What is a Thread and in what situations I should use Threads. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is a Thread and in what situations I should use Threads." Watch "What is a Thread and in what situations I should use Threads." New topic
Author

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

krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
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.


There is no rule that we all should know everything. Lets learn few things of everything here.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
To the Java™ tutorials! See whether that helps.
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
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
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Anywhere where two people need simultaneous access to the same data will require concurrency.
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
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.
Pratyusendu Kumar
Greenhorn

Joined: Mar 30, 2010
Posts: 1
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

Joined: Mar 16, 2010
Posts: 92
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.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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 )


[My Blog]
All roads lead to JavaRanch
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
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

Joined: Mar 16, 2010
Posts: 92
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

Joined: Mar 16, 2010
Posts: 92
Hi Brij Garg, I did not understand what you said.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Mar 16, 2010
Posts: 92
Thank you Chritstophe.
Manohar Puttaswamy
Greenhorn

Joined: Dec 25, 2009
Posts: 2
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.
Rahul Kurup
Greenhorn

Joined: Dec 11, 2009
Posts: 9
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.


Rahul J.
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
Thank you Manohar and Rahul for the examples.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
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
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
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.
Jiji Cherian
Greenhorn

Joined: Aug 10, 2006
Posts: 18
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..
Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
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?


SCJP, SCWCD
Jiji Cherian
Greenhorn

Joined: Aug 10, 2006
Posts: 18
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

Joined: Mar 16, 2010
Posts: 92
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

Joined: Apr 04, 2010
Posts: 42
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

Joined: Mar 16, 2010
Posts: 92
Ok Dude.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
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.
 
GeeCON Prague 2014
 
subject: What is a Thread and in what situations I should use Threads.