views:

207

answers:

6
+1  Q: 

Threads in Java

I was today asked in an interview over the Thread concepts in Java? The Questions were...

  1. What is a thread?
  2. Why do we go for threading?
  3. A real time example over the threads.
  4. Can we create threads in Spring framework service class.
  5. 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.

+1  A: 
  1. Get a good java book. Threading is an essential notion, which you should know about.

  2. I assume "Spring framework service class" is a POJO at first, so yes you can instantiate threads in a POJO, obviously.

  3. About Flex, I don't know enough about it.

Etienne
+1  A: 

I can answer the first 3 since I'm not too familiar with the threading features of Spring or Flex.

  1. A thread is an object that has its own registers, stack and code segment that can run parallel with other threads in a process (a process is a collection of threads).

  2. You write multi-threaded code for the program to be responsive to user interactions. Think of how annoying it would be if you had to wait for your browser to finish downloading a file before you can continue browsing.

  3. I gave an example in #2. Other examples are any programs with a GUI (the GUI has to always be responsive to user input while performing background tasks), or server type software such as a web server where you might have to respond to 1000 requests a minute. It would be better to have a separate thread for each of those responses.

Mike
+2  A: 

To crate threads, create a new class that extends the Thread class, and instantiate that class. The extending class must override the run() method and call start() method to begin execution of the thread.

Inside run(), you will define the code that constitutes a new thread. It is important to understand that run() can call other methods, use other classes and declare variables just like the main thread. The only difference is the run() establishes the entry point for another, concurrent thread of execution within your program. This will end when run() returns.

public class MyThread extends Thread {

  String word;

  public MyThread(String rm){
    word = rm;
  }

  public void run(){

    try {

      for(;;){
        System.out.println(word);
        Thread.sleep(1000);
      }

    } catch(InterruptedException e) {

      System.out.println("sleep intreupted");      
    }
  }

  public static void main(String[] args) {

    Thread t1=new MyThread("First Thread");
    Thread t2=new MyThread("Second Thread");
    t1.start();
    t2.start();
  }
}

Output screen:

First Thread
Second Thread
First Thread
Second Thread
First Thread

and see this :

http://www.javaworld.com/javaworld/jw-04-1996/jw-04-threads.html

and for Spring you can use thread pool :

http://static.springsource.org/spring/docs/2.0.x/reference/scheduling.html

SjB
I have updated my post, can you explain those.
guess it's not about spring scheduling?
TuxGeek
It is better practice to implement Runnable and then pass a Runnable instance to the Thread constructor rather than extend the Thread class.
Adamski
@TiNS: I don't point to spring scheduling i insert link about Scheduling and **Thread Pooling** in **Spring**
SjB
A: 

As far as Spring is concerned, yes you can definitely create your own threads. But it is a better idea to use the thread pool support described in Chapter 25 of the Spring Framework Manual.

However, having requests spawn threads in a Spring-based web service (or any web service for that matter) introduces resource management issues that you may want to avoid ...

Stephen C
+1  A: 

One key concept to clear up is that a thread is an OS scheduling object, which just happens to have a Java class that represents it (as, say, Window would in the UI sub-system). Threads are not themselves a type of class.

pdbartlett
A: 

Flex cannot directly communicate with Java threads, there would have to be some sort of messaging whether you use JMS or whatever, Flex via BlazeDS, GraniteDS, or LCDS can communicate with JMS. One thing to remember as well is that, at the moment anyway, Flash/Flex itself is single threaded, but highly asynchronous....some would say TOO asynchronous...so any use of Java threads to speed things up may not have as great an effect as you might hope for.

mezmo