Skip to content

Commit 3c7a72f

Browse files
java基础-线程
1 parent ef5317f commit 3c7a72f

File tree

7 files changed

+163
-1
lines changed

7 files changed

+163
-1
lines changed

java-core/src/java1/core/thread/local/ThreadLocalTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class ThreadLocalTest {
88
public static void main(String[] args) {
99
for(int i=0;i<2;i++){
1010
new Thread(new Runnable() {
11-
11+
@Override
1212
public void run() {
1313
int data=new Random().nextInt();
1414
System.out.println(Thread.currentThread().getName()+" has put data:"+data);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

0 commit comments

Comments
 (0)