I was today asked in an interview over the Thread concepts in Java? The Questions were…
- What is a thread?
- Why do we go for threading?
- A real time example over the threads.
- Can we create threads in Spring framework service class.
- Can flex call a thread?
I did not answer any questions apart from definition of Thread, that too I just learnt from internet.
Can anyone explain me clearly over this.
Update:
What is a difference between a thread and a normal java class.
why do we need threading… can i execute business logic in threads.
Can i call a different class methods in Threads.
To create threads, create a new class that extends the
Threadclass, and instantiate that class. The extending class must override therunmethod and call thestartmethod to begin execution of the thread.Inside
run, you will define the code that constitutes a new thread. It is important to understand thatruncan call other methods, use other classes and declare variables just like the main thread. The only difference is thatrunestablishes the entry point for another, concurrent thread of execution within your program. This will end whenrunreturns.Here’s an example:
You will see this on the screen:
This tutorial also explains the
Runnableinterface. With Spring, you could use a thread pool.