You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Larry Touve <lt...@potomacfusion.com> on 2010/05/21 15:40:55 UTC

Threads in OSGi

I'm writing a bundle that spawns threads to listen for socket connections, and when connections occur, it spawns off socket reader threads.  After messing around a bit, it appears as though the start() method is acting like the run() method, in that it hangs and doesn't actually create a new thread.   I have the following code in an Activator:

  ListenerThread listener = new ListenerThread(39000);
  listener.start();
  logger.info("Listener started up");

The logger statement never executes.  If I replace it with :

  ListenerThread listener = new ListenerThread(39000);
  Thread thread = new Thread(listener);
  thread.start();
  logger.info("Listener started up");

Everything works as expected.

 My ListenerThread implements Runnable, and I've tried it with extending Thread, but it makes no difference.  I thought that calling the start() method of a class that extends Thread should spawn a new thread.  Is this not the case within OSGi?

Thanks,
 Larry


Re: Threads in OSGi

Posted by Donald Whytock <dw...@gmail.com>.
If ListenerThread implements Runnable, the way to start it is

ListenerThread listener = new ListenerThread(39000);
new Thread(listener).start();

according to the API:

http://java.sun.com/javase/6/docs/api/java/lang/Thread.html

The start() method only works with extensions of Thread.

Don

On Fri, May 21, 2010 at 10:03 AM, Toni Menzel <to...@okidokiteam.com> wrote:
> On Fri, May 21, 2010 at 3:59 PM, Larry Touve <lt...@potomacfusion.com> wrote:
>>> It is the case with osgi too. Nothing differnent to standard java.
>>> What os are you on?
>>>
>>
>> I'm running Windows 7, with Felix 2.0.2 (Glassfish V3.1-SNAPSHOT).
>>
>> In standard Java, thread.start() and thread.run() have different behavior.  The run() method runs the code within the calling thread, and the start() method runs the code in a new thread.  That seems to NOT be the case in OSGi.  In OSGi, both methods execute within the calling thread - at least that's the behavior I'm seeing.
>
> What type is "thread" (in "thread.start") ?  And what exactly is
> ListenerThread ?
> i think you are speaking of different apis. Nothing about osgi (like
> Karl just said).
>
> Toni
>
>
>>
>> Larry
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>>
>
>
>
> --
> Toni Menzel
> Independent Software Developer
> Professional Profile: http://okidokiteam.com
> toni@okidokiteam.com
> http://www.ops4j.org     - New Energy for OSS Communities - Open
> Participation Software.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


RE: Threads in OSGi

Posted by Larry Touve <lt...@potomacfusion.com>.
> Are you sure its using the same thread and not a new thread and the
> old one hangs? If it is a new thread but the old one hangs can you try
> without the felix shell.tui bundle around?
> 
> regards,
> 
> Karl

As usual, it seems I made a mistake.  I must have been using the Runnable implementation when I thought I was using the version that extended Thread.  I double checked it and it looks to be working as it should.

Sorry for the confusion.

Thanks, 
Larry



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Threads in OSGi

Posted by Karl Pauls <ka...@gmail.com>.
Are you sure its using the same thread and not a new thread and the
old one hangs? If it is a new thread but the old one hangs can you try
without the felix shell.tui bundle around?

regards,

Karl

On Fri, May 21, 2010 at 4:12 PM, Larry Touve <lt...@potomacfusion.com> wrote:
>> What type is "thread" (in "thread.start") ?  And what exactly is ListenerThread ?
>> i think you are speaking of different apis. Nothing about osgi (like Karl just said).
>
> I have used both a Thread and a Runnable:
>
> public class ListenerThread extends Thread  // or public class ListenerThread implements Runnable
> {
>    public ListenerThread(int port) throws Exception
>    {
>        serverSocket = new ServerSocket(port);
>    }
>
>    public void run()
>    {
>        while (true)
>        {
>            Socket socket = serverSocket.accept();  // accept connection
>               :
>             (do some stuff here...)
>        }
>    }
> }
>
> The issue is that I get different behavior when I execute this code within OSGi, and outside of OSGi (using the Java 1.6 JVM).
>
> ListenerThread t = new ListenerThread(39000);
> t.start()
>
> Creates a new thread when running in the standard JVM, but when it runs from within OSGi, it runs the ListenerThread code within the calling thread, and effectively suspends the calling thread.
>
>
> Larry
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>



-- 
Karl Pauls
karlpauls@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


RE: Threads in OSGi

Posted by Larry Touve <lt...@potomacfusion.com>.
> What type is "thread" (in "thread.start") ?  And what exactly is ListenerThread ?
> i think you are speaking of different apis. Nothing about osgi (like Karl just said).

I have used both a Thread and a Runnable:

public class ListenerThread extends Thread  // or public class ListenerThread implements Runnable
{
    public ListenerThread(int port) throws Exception
    {
        serverSocket = new ServerSocket(port);
    }  

    public void run()
    {
        while (true)
        {
            Socket socket = serverSocket.accept();  // accept connection
               :
             (do some stuff here...)
        }
    }
}
       
The issue is that I get different behavior when I execute this code within OSGi, and outside of OSGi (using the Java 1.6 JVM).

ListenerThread t = new ListenerThread(39000);
t.start()

Creates a new thread when running in the standard JVM, but when it runs from within OSGi, it runs the ListenerThread code within the calling thread, and effectively suspends the calling thread.

      
Larry 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Threads in OSGi

Posted by Toni Menzel <to...@okidokiteam.com>.
On Fri, May 21, 2010 at 3:59 PM, Larry Touve <lt...@potomacfusion.com> wrote:
>> It is the case with osgi too. Nothing differnent to standard java.
>> What os are you on?
>>
>
> I'm running Windows 7, with Felix 2.0.2 (Glassfish V3.1-SNAPSHOT).
>
> In standard Java, thread.start() and thread.run() have different behavior.  The run() method runs the code within the calling thread, and the start() method runs the code in a new thread.  That seems to NOT be the case in OSGi.  In OSGi, both methods execute within the calling thread - at least that's the behavior I'm seeing.

What type is "thread" (in "thread.start") ?  And what exactly is
ListenerThread ?
i think you are speaking of different apis. Nothing about osgi (like
Karl just said).

Toni


>
> Larry
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>



-- 
Toni Menzel
Independent Software Developer
Professional Profile: http://okidokiteam.com
toni@okidokiteam.com
http://www.ops4j.org     - New Energy for OSS Communities - Open
Participation Software.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


RE: Threads in OSGi

Posted by Larry Touve <lt...@potomacfusion.com>.
> It is the case with osgi too. Nothing differnent to standard java.
> What os are you on?
>

I'm running Windows 7, with Felix 2.0.2 (Glassfish V3.1-SNAPSHOT).

In standard Java, thread.start() and thread.run() have different behavior.  The run() method runs the code within the calling thread, and the start() method runs the code in a new thread.  That seems to NOT be the case in OSGi.  In OSGi, both methods execute within the calling thread - at least that's the behavior I'm seeing.

Larry 



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Threads in OSGi

Posted by Karl Pauls <ka...@gmail.com>.
On Fri, May 21, 2010 at 3:40 PM, Larry Touve <lt...@potomacfusion.com> wrote:
> I'm writing a bundle that spawns threads to listen for socket connections, and when connections occur, it spawns off socket reader threads.  After messing around a bit, it appears as though the start() method is acting like the run() method, in that it hangs and doesn't actually create a new thread.   I have the following code in an Activator:
>
>  ListenerThread listener = new ListenerThread(39000);
>  listener.start();
>  logger.info("Listener started up");
>
> The logger statement never executes.  If I replace it with :
>
>  ListenerThread listener = new ListenerThread(39000);
>  Thread thread = new Thread(listener);
>  thread.start();
>  logger.info("Listener started up");
>
> Everything works as expected.
>
>  My ListenerThread implements Runnable, and I've tried it with extending Thread, but it makes no difference.  I thought that calling the start() method of a class that extends Thread should spawn a new thread.  Is this not the case within OSGi?

It is the case with osgi too. Nothing differnent to standard java.
What os are you on?

regards,

Karl

> Thanks,
>  Larry
>
>



-- 
Karl Pauls
karlpauls@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org