DEV Community

Cover image for Java Quickie. Threads vs Processes
Tristan Elliott
Tristan Elliott

Posted on • Updated on

Java Quickie. Threads vs Processes

Introduction

  • This series is going to be dedicated to the basic understanding of Java. When ever I find myself asking, "How does this work ?". I will create a blog post and put it here. This series will not be in order so feel free to read what ever post you find most relevant. All the resources for this post can be found HERE

Concurrent programming

  • So what is concurrent programming and better yet what does concurrent even mean? Well the great oracle of google tells us that concurrent means, existing, happening, or done at the same time. Ok, but what is concurrent programming? Well, as you navigate your way through the digital world you are able to do multiple things at once, open multiple tabs, listen to Spotify, learn programming and even write sub par blog posts on Dev.to like me. This is all possible because of software that through one way or another has been programmed using concurrent programming techniques. However, to fully understand concurrent programming can be quite tricky but the the first step is understanding what a process and thread are.

  • In concurrent programming there are two basic units of execution

1) Processes : is a self container execution environment. A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space. So technical jargon aside, a process is an execution environment.

1) Threads : Threads are sometimes called lightweight process. Both processes and threads provide an execution environment. Creating a new thread requires fewer resources than creating a process. Threads actually exist within a process and every process has at least one thread. Threads share the process's resources including memory and open files. Initially most applications have just one thread and that thread is universally called the main thread. So if we really had to sum up what a thread is, it is, a execution environment inside of a process that can be used to share the resources of the process

Thread Objects

  • Each thread in Java is associated with an instance of the class Thread. There are two basic strategies for using thread objects to create a concurrent application

1) : Directly control thread creation and management, simply insatiate Thread each time the application needs to initiate an asynchronous task.

2) : To abstract thread management from the rest of your application, pass the application's tasks to an executor. In most cases you will be using this strategy.

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Discussion (0)