Skip to content

Commit 4667417

Browse files
committed
First Commit
1 parent e760b65 commit 4667417

File tree

10 files changed

+6511
-0
lines changed

10 files changed

+6511
-0
lines changed

ThreadPractice/.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"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

ThreadPractice/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

ThreadPractice/.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>ThreadPractice</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>

ThreadPractice/a.txt

Lines changed: 1584 additions & 0 deletions
Large diffs are not rendered by default.

ThreadPractice/b.txt

Lines changed: 1584 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.example.main;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.OutputStream;
9+
import java.util.concurrent.Executor;
10+
import java.util.concurrent.ExecutorService;
11+
import java.util.concurrent.Executors;
12+
13+
import com.example.util.IOUtil;
14+
15+
16+
class WriteData implements Runnable {
17+
String iFile;
18+
String oFile;
19+
20+
21+
public WriteData(String iFile, String oFile) {
22+
this.iFile = iFile;
23+
this.oFile = oFile;
24+
}
25+
26+
27+
@Override
28+
public void run() {
29+
try {
30+
FileInputStream istream = new FileInputStream(iFile);
31+
FileOutputStream ostream = new FileOutputStream(oFile);
32+
System.out.println("Copying File :"+iFile);
33+
IOUtil.copyData(istream, ostream);
34+
} catch (IOException e) {
35+
// TODO Auto-generated catch block
36+
e.printStackTrace();
37+
}
38+
}
39+
40+
}
41+
42+
public class Application {
43+
44+
45+
public static void main(String[] args) throws IOException {
46+
47+
String iFile = "a.txt";
48+
String oFile = "b.txt";
49+
String iFilex = "x.txt";
50+
String oFiley = "y.txt";
51+
52+
/* Thread t1 = new Thread(new WriteData(iFile, oFile));
53+
t1.start();
54+
Thread t2 = new Thread(new WriteData(iFilex, oFiley));
55+
t2.start();*/
56+
57+
// Use executor to define thread count
58+
59+
ExecutorService executor = Executors.newFixedThreadPool(3); // Thread Pool Size
60+
executor.execute(new WriteData(iFile, oFile));
61+
executor.execute(new WriteData(iFilex, oFiley));
62+
System.out.println("DONE");
63+
64+
65+
}
66+
67+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.example.main;
2+
3+
class IncValue{
4+
int x;
5+
6+
7+
public IncValue(int x) {
8+
this.x = x;
9+
}
10+
11+
public int getX() {
12+
return x;
13+
}
14+
15+
public void setX(int x) {
16+
this.x = x;
17+
}
18+
19+
public void incVal(){ // public synchronized void incVal(){
20+
int y = getX();
21+
y++;
22+
// to pause a thread to that other thread can run
23+
try {
24+
Thread.sleep(1);
25+
} catch (InterruptedException e) {
26+
// TODO Auto-generated catch block
27+
e.printStackTrace();
28+
}
29+
setX(y);
30+
31+
}
32+
}
33+
34+
class MyThread extends Thread{
35+
IncValue inv;
36+
37+
public MyThread(IncValue inv) {
38+
this.inv = inv;
39+
}
40+
41+
@Override
42+
public void run() {
43+
inv.incVal();
44+
}
45+
}
46+
47+
public class SyncTest {
48+
49+
public static void main(String[] args) {
50+
IncValue inv = new IncValue(10);
51+
MyThread t1 = new MyThread(inv);
52+
MyThread t2 = new MyThread(inv);
53+
t1.start();
54+
t2.start();
55+
56+
try {
57+
t1.join();
58+
t2.join();
59+
Thread.sleep(1000);
60+
} catch (InterruptedException e) {
61+
// TODO Auto-generated catch block
62+
e.printStackTrace();
63+
}
64+
System.out.println("value of x is:"+inv.getX()); // should be 12 but will come 11.
65+
}
66+
67+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.example.util;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.OutputStream;
6+
7+
public class IOUtil {
8+
9+
public static void copyData(InputStream istream, OutputStream ostream) throws IOException{
10+
11+
int value;
12+
while((value = istream.read()) > -1){
13+
ostream.write(value);
14+
}
15+
}
16+
17+
}

0 commit comments

Comments
 (0)