Need for Thread
When we run a Java application, it executes processes one by one. If one process is waiting for any I/O operation or any other task to complete then the below process will wait till the execution of 1st process. This can be removed by the use of multithreading.
package com.siteinvokers;
public class ThreadBasicRunner {
public static void main(String[] args) {
// Task 1
for (int i = 1; i < 100; i++) {
System.out.print(i + " ");
}
System.out.println("\nTask 1 done ");
// Task 2
for (int i = 101; i < 200; i++) {
System.out.print(i + " ");
}
System.out.println("\nTask 2 done ");
// Task 3
for (int i = 201; i < 300; i++) {
System.out.print(i + " ");
}
System.out.println("\nTask 3 done ");
System.out.println(" main done ");
}
}
// Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
Task 1 done
101 102 103 104 105 106 107 108 109 110 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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
Task 2 done
201 202 203 204 205 206 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
Task 3 done
main done
What is Thread
Threads are lightweight subprocesses that have independent courses of execution and are the smallest unit of a process. Even though these threads share memory, they operate independently of one another, so when one thread fails, it doesn’t interfere with the other threads’ ability to function.
States Of Thread
- New
- Runnable
- Running
- Blocked/Waiting
- Terminated/Dead
Implemeting thread using Extends Thread
We can run Threads in Java by using Thread Class, which provides constructors and methods for creating and performing operations on a
Code
package com.siteinvokers;
// Thread using extends Thread
class Task1 extends Thread {
public void run() { // It matches the exact signature
System.out.println("\nTask 1 started ");
for (int i = 1; i < 50; i++) {
System.out.print(i + " ");
}
System.out.println("\nTask 1 done ");
}
}
public class ThreadBasicRunner {
public static void main(String[] args) {
// Task 1
Task1 task1 = new Task1();
task1.start(); // starting the task1
// Task 2
System.out.println("\nTask 2 started ");
for (int i = 101; i < 150; i++) {
System.out.print(i + " ");
}
System.out.println("\nTask 2 done ");
// Task 3
System.out.println("\nTask 3 started ");
for (int i = 201; i < 250; i++) {
System.out.print(i + " ");
}
System.out.println("\nTask 3 done ");
System.out.println(" main done ");
}
}
// Output
Task 2 started
Task 1 started
1 2 101 102 103 104 105 106 107 3 108 4 5 109 110 6 7 8 111 112 9 10 113 114 11 12 115 116 13 14 117 15 16 118 119 17 18 120 19 20 121 122 21 22 123 124 23 24 125 126 25 26 127 128 27 28 129 130 29 30 131 132 31 32 133 134 33 34 135 136 35 36 137 138 37 38 139 140 39 40 141 41 42 142 143 43 44 144 45 46 145 146 47 48 147 148 49 149
Task 2 done
Task 3 started
201 202 203
Task 1 done
204 205 206 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
Task 3 done
main done
Implemeting thread using Runnable interface
Thread, which extends a Thread class that can implement Runnable Interface. We use the following constructors for creating the Thread:
- Thread
- Thread(Runnable r)
- Thread(String name)
- Thread(Runnable r, String name)
Code
package com.siteinvokers;
// Thread using extends Thread
class Task1 extends Thread {
public void run() { // It matches the exact signature
System.out.println("\nTask 1 started ");
for (int i = 1; i < 50; i++) {
System.out.print(i + " ");
}
System.out.println("\nTask 1 done ");
}
}
// Implementing runnable interface
class Task2 implements Runnable {
@Override
public void run() {
System.out.println("\nTask 2 started ");
for (int i = 101; i < 150; i++) {
System.out.print(i + " ");
}
System.out.println("\nTask 2 done ");
}
}
public class ThreadBasicRunner {
public static void main(String[] args) {
// Task 1
Task1 task1 = new Task1();
task1.start(); // starting the task1
// Task 2
Task2 task2 = new Task2();
Thread task2thread = new Thread(task2);
task2thread.start();
// Task 3
System.out.println("\nTask 3 started ");
for (int i = 201; i < 250; i++) {
System.out.print(i + " ");
}
System.out.println("\nTask 3 done ");
System.out.println(" main done ");
}
}
// Output
Task 1 started
Task 2 started
Task 3 started
101 201 1 202 102 203 2 204 103 205 3 206 104 207 4 208 105 209 5 210 106 211 6 212 107 213 7 214 108 215 8 216 109 217 9 218 110 219 10 220 111 221 11 222 112 223 12 224 113 225 13 226 114 227 14 228 115 229 15 230 116 231 16 232 117 233 17 234 118 235 18 236 119 237 19 238 239 120 240 20 241 121 242 122 123 21 124 243 125 22 126 244 23 245 24 25 127 26 246 27 128 247 129 130 28 131 248 132 29 133 249 134 30
Task 3 done
31 32 135 33 34 main done
35 136 36 137 138 37 38 139 39 40 41 140 42 43 44 141 45 142 46 143 144 47 48 145 146 49 147
Task 1 done
148 149
Task 2 done