File tree 8 files changed +144
-4
lines changed
src/com/shohagh/java/Thread
8 files changed +144
-4
lines changed Original file line number Diff line number Diff line change 6
6
<attribute name =" maven.pomderived" value =" true" />
7
7
</attributes >
8
8
</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 " >
10
10
<attributes >
11
11
<attribute name =" maven.pomderived" value =" true" />
12
12
</attributes >
Original file line number Diff line number Diff line change 1
1
eclipse.preferences.version =1
2
2
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
5
5
org.eclipse.jdt.core.compiler.problem.assertIdentifier =error
6
6
org.eclipse.jdt.core.compiler.problem.enumIdentifier =error
7
7
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments