# java.lang.OutOfMemoryError: unable to create new native thread

**URL:** <https://discuss.akka.io/t/java-lang-outofmemoryerror-unable-to-create-new-native-thread/10606>\
**Category:** Akka Libraries\
**Created:** [January 25, 2024, 11:05am UTC](https://discuss.akka.io/t/java-lang-outofmemoryerror-unable-to-create-new-native-thread/10606 "2024-01-25T11:05:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mghildiy1](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mghildiy1](https://discuss.akka.io/u/mghildiy1)\
**Post date:** [January 25, 2024, 11:05am UTC](https://discuss.akka.io/t/java-lang-outofmemoryerror-unable-to-create-new-native-thread/10606/1 "2024-01-25T11:05:04Z")

</div>

I have an actor that is created as:

`context.actorOf(MyActor.props)`

Inside MyActor, some remote service calls are made to fetch data as part of receive method.

Additionally a thread is spawn when actor is created to perform some additional work:

```auto
val myThread = new Thread() {
   ......
   ......
    override def run(): Unit = {
          running = true
          while (running) {
            performTask(....)
            Thread.sleep(10000)
          }
        }
}
myThread .start()
....

```

Is this way of doing things inside an actor a good practise?

Problem I am facing is that soemtimes I see error:

```auto
java.lang.OutOfMemoryError: unable to create new native thread
	at java.lang.Thread.start0(Native Method)

```

Any lead is welcome.

---

<div class="post-metadata">

**Author:** ![octonato](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/octonato/32/2885_2.png) [@octonato](https://discuss.akka.io/u/octonato)\
**Post date:** [January 26, 2024, 10:12am UTC](https://discuss.akka.io/t/java-lang-outofmemoryerror-unable-to-create-new-native-thread/10606/2 "2024-01-26T10:12:53Z")

</div>

Hi @mghildiy1,

No, that’s not a good practice.

Starting threads are very costly.  
Both Java and Scala offers APIs to create thread pools. Instead of spawning threads yourself, you should use CompletionStage (java) or a Future (scala).

I see you are using Scala, so maybe the best is to get familiar with Futures and ExecutionContext in Scala first.

Once you get a good understanding of how you should use them, you should look at how to use Futures inside Actors. Here is a good place to start: [Interaction Patterns • Akka Documentation](https://doc.akka.io/docs/akka/current/typed/interaction-patterns.html)  
But first make sure you get familiar with Futures in Scala.

Regards,

Renato

---

<div class="post-metadata">

**Author:** ![hepin1989](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/hepin1989/32/398_2.png) [@hepin1989](https://discuss.akka.io/u/hepin1989)\
**Post date:** [January 29, 2024, 7:51am UTC](https://discuss.akka.io/t/java-lang-outofmemoryerror-unable-to-create-new-native-thread/10606/3 "2024-01-29T07:51:36Z")

</div>

Think actor as a fiber, so there is no need to create the Thread. you need a dedicated thread pool to manage your blocking or use VirtualThread.
