File tree Expand file tree Collapse file tree 5 files changed +74
-0
lines changed
Expand file tree Collapse file tree 5 files changed +74
-0
lines changed Original file line number Diff line number Diff line change 1+ package ConcurrencyPatterns .TS_Singleton ;
2+
3+ public class Singleton {
4+ private static volatile Singleton instance ;
5+
6+ private Singleton (){}
7+
8+ public static Singleton getInstance (){
9+ if (instance == null ){
10+ synchronized (Singleton .class ){
11+ if (instance == null ){
12+ instance = new Singleton ();
13+ }
14+ }
15+ }
16+ return instance ;
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ package ConcurrencyPatterns .ThreadPool ;
2+
3+ import java .util .concurrent .ExecutorService ;
4+ import java .util .concurrent .Executors ;
5+ import java .util .Timer ;
6+ import java .util .TimerTask ;
7+
8+ public class Main {
9+ public static void main (String [] args ) {
10+ ExecutorService executorService = Executors .newFixedThreadPool (3 );
11+ for (int i = 0 ; i < 10 ; i ++) {
12+ executorService .execute (new TestRunnable ());
13+ }
14+ executorService .shutdown ();
15+ }
16+ }
17+
18+ class TestRunnable implements Runnable {
19+ public void run () {
20+ synchronized (System .out ){
21+ System .out .println ("Running" );
22+ }
23+ try {
24+ Thread .sleep (2000 );
25+ synchronized (System .out ){
26+ System .out .println ("Finished Running" );
27+ }
28+ } catch (InterruptedException e ) {
29+ throw new RuntimeException (e );
30+ }
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ package ConcurrencyPatterns .Thread_Adapter ;
2+
3+ public class C {
4+ public void func (){
5+ System .out .println ("Hello!" );
6+ }
7+ }
Original file line number Diff line number Diff line change 1+ package ConcurrencyPatterns .Thread_Adapter ;
2+
3+ public class Main {
4+ public static void main (String [] args ){
5+ RunnableC runnable = new RunnableC ();
6+ Thread thread = new Thread (runnable );
7+ thread .start ();
8+ }
9+ }
Original file line number Diff line number Diff line change 1+ package ConcurrencyPatterns .Thread_Adapter ;
2+
3+ public class RunnableC extends C implements Runnable {
4+ @ Override
5+ public void run () {
6+ func ();
7+ }
8+ }
You can’t perform that action at this time.
0 commit comments