Skip to content

Commit 8f7a58e

Browse files
Added all example
0 parents  commit 8f7a58e

20 files changed

+198
-0
lines changed

.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>JavaThread</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
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.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
Binary file not shown.
Binary file not shown.
Binary file not shown.
837 Bytes
Binary file not shown.
861 Bytes
Binary file not shown.
1.06 KB
Binary file not shown.
952 Bytes
Binary file not shown.
Binary file not shown.
723 Bytes
Binary file not shown.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.javathread.anonymous;
2+
3+
public class AnonymousMainClass {
4+
5+
public static void main(String[] args) {
6+
7+
new Thread(new Runnable() {
8+
9+
@Override
10+
public void run() {
11+
try {
12+
for (int i = 0; i < 5; i++) {
13+
System.out.println("I am an anonymous Thread");
14+
Thread.sleep(3000);
15+
}
16+
} catch (InterruptedException e) {
17+
e.printStackTrace();
18+
}
19+
}
20+
}).start();
21+
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.javathread.extending;
2+
3+
4+
public class ExtendingMainClass {
5+
public static void main(String[] args) {
6+
ExtendingThread thread = new ExtendingThread();
7+
thread.start();
8+
9+
// If you want to see how each thread works, you can
10+
// uncover it
11+
/*try {
12+
for (int i = 0; i < 5; i++) {
13+
System.out.println("From Main Class Thread");
14+
Thread.sleep(5000);
15+
}
16+
} catch (InterruptedException e) {
17+
e.printStackTrace();
18+
}*/
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.javathread.extending;
2+
3+
public class ExtendingThread extends Thread {
4+
5+
@Override
6+
public void run() {
7+
super.run();
8+
try {
9+
for (int i = 0; i < 5; i++) {
10+
System.out.println("I am an extending thread");
11+
sleep(3000);
12+
}
13+
14+
} catch (InterruptedException e) {
15+
e.printStackTrace();
16+
}
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.javathread.runnable;
2+
3+
4+
public class RunnableClass implements Runnable {
5+
6+
@Override
7+
public void run() {
8+
try {
9+
for (int i = 0; i < 5; i++) {
10+
System.out.println("Im from Runnable Interface");
11+
Thread.sleep(3000);
12+
}
13+
} catch (InterruptedException e) {
14+
e.printStackTrace();
15+
}
16+
}
17+
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.javathread.runnable;
2+
3+
import java.util.logging.Handler;
4+
5+
6+
public class RunnableMain {
7+
public static void main(String[] args) {
8+
/* You can only use this or
9+
* below code for this solution
10+
*
11+
* try {
12+
for (int i = 0; i < 5; i++) {
13+
System.out.println("asf");
14+
Thread.sleep(2000);
15+
}
16+
} catch (InterruptedException e) {
17+
e.printStackTrace();
18+
}*/
19+
20+
RunnableClass runnable = new RunnableClass();
21+
new Thread(runnable).start();
22+
23+
try {
24+
for (int i = 0; i < 5; i++) {
25+
System.out.println("I am from runnable main");
26+
Thread.sleep(3000);
27+
}
28+
} catch (InterruptedException e) {
29+
e.printStackTrace();
30+
}
31+
}
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.javathread.synchronize;
2+
3+
public class MyThread extends Thread {
4+
private int threadId;
5+
private TargetClass target;
6+
7+
public MyThread(int threadId, TargetClass target) {
8+
this.threadId = threadId;
9+
this.target = target;
10+
}
11+
12+
@Override
13+
public void run() {
14+
super.run();
15+
16+
synchronized (target) {
17+
try {
18+
sleep(2000);
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
target.call(threadId);
23+
}
24+
25+
}
26+
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.javathread.synchronize;
2+
3+
public class SynchronizeMain {
4+
public static void main(String[] args) {
5+
TargetClass target = new TargetClass();
6+
7+
MyThread t1 = new MyThread(1, target);
8+
MyThread t2 = new MyThread(2, target);
9+
MyThread t3 = new MyThread(3, target);
10+
11+
t1.start();
12+
t2.start();
13+
t3.start();
14+
}
15+
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.javathread.synchronize;
2+
3+
public class TargetClass {
4+
5+
public void call(int threadId)
6+
{
7+
System.out.println("Calling from Thread"+ threadId);
8+
}
9+
10+
}

0 commit comments

Comments
 (0)