Announcement Announcement Module
No announcement yet.
java.lang.Thread Example Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • java.lang.Thread Example

    I need a full java.lang.Thread example that is in the spring context.
    I want to have some thread instances that can run simultaneously, and these instances shall be spring beans.
    I tried to create some instances like


    <bean id="threadInstance1" class="example.ThreadInstance1">
    <property name="field1"><ref bean="anotherBean1"/></property>

    <bean id="threadInstance2" class="example.ThreadInstance2">
    <property name="field1"><ref bean="anotherBean1"/></property>
    <property name="field2"><ref bean="anotherBean2"/></property>


    and these instance classes extends java.lang.Thread.
    But in these case, you can use each Thread object once since thay are singleton, after instance finishes its job, the thread becomes TERMINATED, so you can not use it again.

    I tried to use ThreadLocal object but I cant find a fully example about it so i cant manage to run an example successfully.

    Any idea will be very helpful to me.

  • #2
    Not sure I understand the question, but creating threads and beans are not related exactly. For instance the following will create a thread per each Spring singleton:

       <bean id="thread1" class="Threader" init-method="init">
           <property name="title" value="x"/>
       <bean id="thread2" class="Threader" init-method="init">  
           <property name="title" value="y"/>
    public class Threader extends Thread {
      private String title;
      public void run() {
          System.out.println("Running..." + this.currentThread().getName());
      public void init(){
        Threader t = new Threader();
    **** post edit ****
    Of course, Spring may have an easier way of doing this. Or, you can just use Spring scheduling.
    Last edited by jbetancourt; Sep 5th, 2006, 11:58 AM.


    • #3
      OK, but i want to use these threads more than once, i mean i want to create a multithreaded application by these thread beans, but if i create these thread beans, after i use them once, then they become useless, since their state becomes TERMINATED (Since they are singleton). And i cant change their state manually after they beceome TERMINATED, ie. I have to create them again during application process, but I cant since they are spring beans.

      Can you give me a full example about Spring scheduling?



      • #4
        Sounds like what you need is a thread pool? You may want to look at the
        Scheduling and Thread Pooling in the reference manual. In particular, Spring 1.0 introduced the TaskExecutor abstraction. I have not used it so can't comment on its applicability to your case.

        23.4.2. Where to use a TaskExecutor

        The TaskExecutor was originally created to give other Spring components an abstraction for thread pooling where needed. Components such as the ApplicationEventMulticaster, JMS's AbstractMessageListenerContainer, and Quartz integration all use the TaskExecutor abstraction to pool threads. However, if your beans need thread pooling behavior, it is possible to use this abstraction for your own needs.
        In the reference manual there is also the "7.10.2. Pooling target sources" which discusses the


        • #5
          Thank you,
          Task executor solved my problem, but i was using spring framework 1.2.8
          I had to upgrade it to 2.0, that was nice also