-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22012023.java
More file actions
126 lines (108 loc) · 3.51 KB
/
Copy path22012023.java
File metadata and controls
126 lines (108 loc) · 3.51 KB
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
100
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
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
/*
* Java Basic
* designPattern: singleton
*
*
*
*/
// public class Singleton{
// public static void main(String args[]){
// Singleton.statics.Singleton instance = Singleton.getInstance();
// }
// // innerclass
// public static void main(String args[]){
// Singleton.staticInnerClass.Singleton instance2 = Singleton.staticInnerClass.Singleton.getInstance();
// }
// // thread safety
// public static void main(String args[]){
// Singleton.threadSafety.Singleton instance3 = Singleton.threadSafety.Singleton.getInstance();
// }
// // thread safety volatile
// public static void main(String args[]){
// Singleton.threadSafetyVolatile.Singleton instance3 = Singleton.threadSafetyVolatile.Singleton.getInstance();
// }
// }
public class RunableThread implements Runnable {
public int count =0;
// must define a method call run()
public void run(){
System.out.println("RunnableThread starting...");
try {
while(count<5){
Thread.sleep(500);
System.out.println("In Thread, count is"+ count);
count++;
}
} catch (InterruptedException e){
System.out.println("RunnableThread Interrupted.");
}
System.out.println("RunnableThread terminating.");
}
public static void main(String[] args){
RunableThread instance= new RunableThread();
Thread thread =new Thread(instance);
while(instance.count!=5){
try{
Thread.sleep(250);
} catch(InterruptedException e){
e.printStackTrace();// standard error stream
}
}
}
}
public class ExtendThread extends Thread {
int count =0;
// override run method
public void run(){
try{
while(count<5){
Thread.sleep(250);
System.out.print("In Thread,count is"+count);
count++;
}
} catch(InterruptedException e){
System.out.println("Thread Interrupted");
}
System.out.println("Thread terminating");
}
}
public class ExtendThreadRun{
public static void main(String args[]){
ExtendThread instance= new ExtendThread();
instance.start();// will call the run method to start the thread
while(instance.count!=5){
try{
Thread.sleep(250);
} catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
// Java Thread POOL
public class ThreadPoolTest {
public static void main(String[] args) throws Exception {
Scacnner in=new Scanner(System.in);
System.out.println("Enter base directory");
String directory= in.nextLine();
System.out.print("Enter keyword");
String keyword= in.nextLine();
ExecutorService pool=Executors.newCachedThreadPool();
MatchCounter counter=new MatchCounter(new File(directory),keyword,pool);
Future<Integer> result=pool.submit(counter);
try{
System.out.println(result.get()+"matching files.");
}
catch{
e.printStackTrace();
}
catch(InterruptedException e){
}
pool.shutdown();
int largestPoolSize=((ThreadPoolExecutor) pool).getLargestPoolSize();
}
}