0

I want to see how Jboss creates/assign different threads from Thread pool once it finds previous thread busy.For that i tried to write down a code I hope making a thread sleep will make it busy and Jboss will create a new one. But it didnt work. I want my Test0 class to create 5 threads to execute run method of Test1 whenever it finds Test1 thread is busy in doing something.

public class Test1 extends Thread{
   public Test1(){
    System.out.println("T1 Constructor");
}

@Override
   public void run() {

   System.out.println("run from t1 "+ Thread.currentThread().getName());
   try {
    Thread.currentThread().sleep(5000);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }

}

}

And I have Test0 class which will execute when jboss will start as follows

@Singleton
@Startup
public class Test0 {
private Test1 t1;
public Test0(){

}

@PostConstruct
public void starts(){
    for (int i=0;i<5;i++){
    t1=new Test1();
    t1.start();
    try {
        Thread.sleep(1000);

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
  }
}

Now in Test0 class I am manully creating 5 threads . How should i format the code to have Jboss create Thread from Thread pool?

Will it make any difference if i call t1.run() instead of t1.start() while running on servers? Because i know t1.run will not create a new thread but so this still holds same in case of servers as well?

Mohit Bhagat
  • 245
  • 5
  • 14
  • Why not create a Servlet and invoke it repeatedly? That should create different threads. – Marco A. Hernandez Jun 08 '16 at 15:18
  • no the main thing is i want to find a logic n correct way because in some project i saw same structure of code – Mohit Bhagat Jun 08 '16 at 17:13
  • It doesn't 'find previous threads busy'. It finds it has a new task for which it needs a thread, so it gets one from the pool. It is the pool that knows which threads are free. You can't use the JBoss thread pool yourself AFAIK. – user207421 Jun 09 '16 at 05:36
  • Check this question and answers: http://stackoverflow.com/questions/533783/why-is-spawning-threads-in-java-ee-container-discouraged – Federico Sierra Jun 09 '16 at 12:29

1 Answers1

0

If i understand your question correctly you want to know when a new thread is used from Jboss thread pool. How are you checking if JBoss has spawned a new thread? Is it you looking at the Jboss log. Jboss will start a new thread only if a new client request is sent to Jboss server while existing thread is already busy executing prev request. Fire multiple client request to Jboss server to see multiple threads processing your code at a given point of time.

  • so in my code t1=new Test1(); wont create new Threads ? How can i pass multiple request to Jboss , I am new to Jboss and also not much experienced in threading. – Mohit Bhagat Jun 09 '16 at 08:06
  • This will create new threads but it is a thread created by your code not by Jboss. so creating threads like this will not impact Jboss. To impact Jboss thread pool you have to sent multiple http request simultaneously. – user3395036 Jun 14 '16 at 04:58