Difference Between Running and Runnable States of a Thread in Java - GeeksforGeeks

22 Nov, 2021
Thread is the backbone of multithreading in java. Multithreading is a feature that allows concurrent execution of two or more parts of the program for the maximum utilization of CPU.  Each part of such a program is called a thread. So threads are light-weighted processes within a process.
A thread can have multiple states in Java and lies in any one of the following states at any time of execution
New
Runnable
Running
Waiting/Blocked
Terminated/Dead
The runnable state of a thread is a state in which the thread is ready to run is said to be in a Runnable state or in other words waiting for other threads (currently executing) to complete its execution and execute itself. Running State of a thread where the currently executing in the processor is said to in a Running state. It is the responsibility of the thread scheduler to give the thread, time to run.
A multi-threaded program allocates a fixed amount of time to each individual thread. Each and every thread runs for a short while and then pauses and relinquishes the CPU to another thread so that other threads can get a chance to run.
Here we will be discussing the differences between Runnable and Running states as most of the learning programmers get confused in both these states. Below is the program been provided for the better’s sake of clarity and internal working.
Example:
Java
// java Program to illustrate Difference between// Running and Runnable states of Thread // Importing input output classesimport java.io.*; // Class 1// Helper Class (extending main Thread class)// Defining Thread1class Thread1 extends Thread {     // run() method for Thread1    public void run()    {         // Display message only when thread1 starts        System.out.println("Thread 1 started ");         // Iterations        for (int i = 101; i < 200; i++)            System.out.print(i + " ");         // Display message only when thread1 ended        System.out.println("\nThread 1 completed");    }} // Class 2// Helper Class (extending main Thread class)// Defining Thread2class Thread2 extends Thread {     // run() method for Thread 2    public void run()    {         // Display message only when thread 2 starts        System.out.println("Thread 2 started ");         // Iterations        for (int i = 201; i < 300; i++)            System.out.print(i + " ");         // Display message only when thread 2 ended        System.out.println("\nThread 2 completed");    }} // Class 3// Helper Class (extending main Thread class)// Defining Thread3class Thread3 extends Thread {     // run() method for Thread 3    public void run()    {         // Display message only when thread 3 starts        System.out.println("Thread 3 started ");         // Iterations        for (int i = 301; i < 400; i++)            System.out.print(i + " ");         // Display message only when thread 3 starts        System.out.println("\nThread 3 completed");    }} // Class 4// Main Classclass GFG {     // Main driver method    public static void main(String[] args)    {         // Try block to check for exceptions        try {             // Creating object of each of the threads            // defined            Thread1 thread1 = new Thread1();            Thread2 thread2 = new Thread2();            Thread3 thread3 = new Thread3();             // Instructing thread to start the execution            // using the start() method            thread1.start();            thread2.start();            thread3.start();        }         // Catch block to handle the exceptions        catch (Exception e) {             // Print the line number where exception            // occured            e.printStackTrace();        }    }}
Output:
Thread 1 started Thread 2 started Thread 3 started 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 Thread 3 completed 101 102 103 104 201 105 202 106 107 203 108 204 109 205 110 206 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 Thread 2 completed 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 Thread 1 completed
Note: The Output of the above code is not necessarily the same for every time we execute, since it depends on the CPU ( thread scheduler) which thread is allocated with the processor and for how much time.
Output Explanation:
In order to understand this in the context of the above program consider when the 301 statement is being printed it means thread3 is in running state, but what is the state of thread1 and thread2, the answer is, in the meanwhile when thread3 is being executed in the processor, thread2 and thread1 are waiting for their turn to be processed or executed, i.e. they currently are in Runnable state (or ready to run).
 
akshaysingh98088
sooda367
Java-Multithreading
Difference Between
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Prim's and Kruskal's algorithm for MST
Difference between Internal and External fragmentation
Differences and Applications of List, Tuple, Set and Dictionary in Python
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Arrays.sort() in Java with examples
Reverse a string in Java