Skip to content

Commit a3cd30d

Browse files
author
shohaghcsesust
committed
Practised thread for both Thread and Runnable
1 parent 765baad commit a3cd30d

8 files changed

+144
-4
lines changed

.classpath

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<attribute name="maven.pomderived" value="true"/>
77
</attributes>
88
</classpathentry>
9-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
9+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
1010
<attributes>
1111
<attribute name="maven.pomderived" value="true"/>
1212
</attributes>

.settings/org.eclipse.jdt.core.prefs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
eclipse.preferences.version=1
22
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3-
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4-
org.eclipse.jdt.core.compiler.compliance=1.7
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.compliance=1.8
55
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
66
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
77
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
8-
org.eclipse.jdt.core.compiler.source=1.7
8+
org.eclipse.jdt.core.compiler.source=1.8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.shohagh.java.Thread;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
public class ExtendedThread extends Thread {
6+
@Override
7+
public void run() {
8+
for (int i = 0; i < 5; i++) {
9+
System.out.println("[" + i + "] Inside "
10+
+ Thread.currentThread().getName());
11+
sleepOneSecond();
12+
}
13+
}
14+
15+
private static void sleepOneSecond() {
16+
try {
17+
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.shohagh.java.Thread;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
public class ExtendedThreadDemo {
6+
7+
public static void main(String[] args) {
8+
// TODO Auto-generated method stub
9+
ExtendedThread extendedThread = new ExtendedThread();
10+
extendedThread.setName("Extended Thread");
11+
extendedThread.start();
12+
13+
for (int i = 0; i < 5; i++) {
14+
System.out.println("[" + i + "] Inside "
15+
+ Thread.currentThread().getName());
16+
sleepOneSecond();
17+
}
18+
}
19+
20+
private static void sleepOneSecond() {
21+
try {
22+
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
23+
} catch (InterruptedException e) {
24+
e.printStackTrace();
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.shohagh.java.Thread;
2+
3+
public class MainThreadExample {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
Thread currentThread = Thread.currentThread();
8+
System.out.println("Current thread name: "+currentThread.getName());
9+
}
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.shohagh.java.Thread;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
public class MyThreadRunnable implements Runnable {
6+
7+
@Override
8+
public void run() {
9+
// TODO Auto-generated method stub
10+
for (int i = 0; i < 5; i++) {
11+
System.out.println("[" + i + "] Inside "
12+
+ Thread.currentThread().getName());
13+
sleepOneSecond();
14+
}
15+
}
16+
17+
private static void sleepOneSecond() {
18+
try {
19+
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
20+
} catch (InterruptedException e) {
21+
e.printStackTrace();
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.shohagh.java.Thread;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
public class RunnableThreadDemo {
6+
7+
public static void main(String[] args) {
8+
// TODO Auto-generated method stub
9+
MyThreadRunnable myThreadRunnable = new MyThreadRunnable();
10+
Thread thread = new Thread(myThreadRunnable);
11+
thread.setName("Runnable Thread");
12+
thread.start();
13+
14+
for (int i = 0; i < 5; i++) {
15+
System.out.println("[" + i + "] Inside "
16+
+ Thread.currentThread().getName());
17+
sleepOneSecond();
18+
}
19+
}
20+
21+
private static void sleepOneSecond() {
22+
try {
23+
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
24+
} catch (InterruptedException e) {
25+
e.printStackTrace();
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.shohagh.java.Thread;
2+
3+
public class ThreadDemo {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
//using anonymous class
8+
Thread t1 = new Thread(new Runnable() {
9+
@Override
10+
public void run() {
11+
// TODO Auto-generated method stub
12+
doWork();
13+
}
14+
});
15+
t1.start();
16+
17+
//using lambda expression
18+
Thread t2 = new Thread( () -> {
19+
doWork();
20+
});
21+
t2.start();
22+
}
23+
24+
private static void doWork() {
25+
System.out.println("Doing some important work!");
26+
}
27+
28+
}

0 commit comments

Comments
 (0)