File tree Expand file tree Collapse file tree 7 files changed +163
-1
lines changed
java-core/src/java1/core/thread Expand file tree Collapse file tree 7 files changed +163
-1
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ public class ThreadLocalTest {
8
8
public static void main (String [] args ) {
9
9
for (int i =0 ;i <2 ;i ++){
10
10
new Thread (new Runnable () {
11
-
11
+ @ Override
12
12
public void run () {
13
13
int data =new Random ().nextInt ();
14
14
System .out .println (Thread .currentThread ().getName ()+" has put data:" +data );
Original file line number Diff line number Diff line change
1
+ package java1 .core .thread .lock ;
2
+
3
+ /**
4
+ * <p></p>
5
+ *
6
+ * @author jwzhao
7
+ * @version 1.0
8
+ * @date 2019/3/26 15:20
9
+ */
10
+ public class ThreadA extends Thread {
11
+
12
+ private ThreadService threadService ;
13
+
14
+ public ThreadA (ThreadService threadService ){
15
+ super ();
16
+ this .threadService = threadService ;
17
+ }
18
+
19
+ @ Override
20
+ public void run () {
21
+ threadService .methodA ();
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ package java1 .core .thread .lock ;
2
+
3
+ /**
4
+ * <p></p>
5
+ *
6
+ * @author jwzhao
7
+ * @version 1.0
8
+ * @date 2019/3/26 15:20
9
+ */
10
+ public class ThreadAA extends Thread {
11
+
12
+ private ThreadService threadService ;
13
+
14
+ public ThreadAA (ThreadService threadService ){
15
+ super ();
16
+ this .threadService = threadService ;
17
+ }
18
+
19
+ @ Override
20
+ public void run () {
21
+ threadService .methodA ();
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ package java1 .core .thread .lock ;
2
+
3
+ /**
4
+ * <p></p>
5
+ *
6
+ * @author jwzhao
7
+ * @version 1.0
8
+ * @date 2019/3/26 15:20
9
+ */
10
+ public class ThreadB extends Thread {
11
+
12
+ private ThreadService threadService ;
13
+
14
+ public ThreadB (ThreadService threadService ){
15
+ super ();
16
+ this .threadService = threadService ;
17
+ }
18
+
19
+ @ Override
20
+ public void run () {
21
+ threadService .methodB ();
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ package java1 .core .thread .lock ;
2
+
3
+ /**
4
+ * <p></p>
5
+ *
6
+ * @author jwzhao
7
+ * @version 1.0
8
+ * @date 2019/3/26 15:20
9
+ */
10
+ public class ThreadBB extends Thread {
11
+
12
+ private ThreadService threadService ;
13
+
14
+ public ThreadBB (ThreadService threadService ){
15
+ super ();
16
+ this .threadService = threadService ;
17
+ }
18
+
19
+ @ Override
20
+ public void run () {
21
+ threadService .methodB ();
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ package java1 .core .thread .lock ;
2
+
3
+ /**
4
+ * <p></p>
5
+ *
6
+ * @author jwzhao
7
+ * @version 1.0
8
+ * @date 2019/3/26 15:27
9
+ */
10
+ public class ThreadRun {
11
+
12
+ public static void main (String [] args ) throws InterruptedException {
13
+ ThreadService threadService = new ThreadService ();
14
+ ThreadA threadA = new ThreadA (threadService );
15
+ threadA .setName ("methodA" );
16
+ threadA .start ();
17
+ ThreadAA threadAA = new ThreadAA (threadService );
18
+ threadAA .setName ("methodAA" );
19
+ threadAA .start ();
20
+ Thread .sleep (2000 );
21
+ ThreadB threadB = new ThreadB (threadService );
22
+ threadB .setName ("threadB" );
23
+ threadB .start ();
24
+ ThreadBB threadBB = new ThreadBB (threadService );
25
+ threadBB .setName ("threadBB" );
26
+ threadBB .start ();
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ package java1 .core .thread .lock ;
2
+
3
+ import java .util .concurrent .locks .Lock ;
4
+ import java .util .concurrent .locks .ReentrantLock ;
5
+
6
+ /**
7
+ * <p></p>
8
+ *
9
+ * @author jwzhao
10
+ * @version 1.0
11
+ * @date 2019/3/26 15:16
12
+ */
13
+ public class ThreadService {
14
+
15
+ private Lock lock = new ReentrantLock ();
16
+
17
+ public void methodA (){
18
+ try {
19
+ lock .lock ();
20
+ System .out .println ("methodA begin ThreadName=" +Thread .currentThread ().getName ()+" time= " +System .currentTimeMillis ());
21
+ Thread .sleep (5000 );
22
+ System .out .println ("methodA begin ThreadName=" +Thread .currentThread ().getName ()+" time= " +System .currentTimeMillis ());
23
+ }catch (InterruptedException e ){
24
+ e .printStackTrace ();
25
+ }finally {
26
+ lock .unlock ();
27
+ }
28
+ }
29
+
30
+ public void methodB (){
31
+ try {
32
+ lock .lock ();
33
+ System .out .println ("methodB begin ThreadName=" +Thread .currentThread ().getName ()+" time= " +System .currentTimeMillis ());
34
+ Thread .sleep (5000 );
35
+ System .out .println ("methodB begin ThreadName=" +Thread .currentThread ().getName ()+" time= " +System .currentTimeMillis ());
36
+ }catch (InterruptedException e ){
37
+ e .printStackTrace ();
38
+ }finally {
39
+ lock .unlock ();
40
+ }
41
+ }
42
+ }
You can’t perform that action at this time.
0 commit comments