-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package thread_demo.demo43; | ||
|
||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
public class MyService { | ||
|
||
private ReentrantLock lock; | ||
|
||
public MyService(boolean isFair) { | ||
this.lock = new ReentrantLock(isFair); | ||
} | ||
|
||
public void method1() { | ||
System.out.println("thread name : " + Thread.currentThread().getName() + " 尝试获取锁, at " + System.currentTimeMillis()); | ||
lock.lock(); | ||
System.out.println("thread name : " + Thread.currentThread().getName() + " 获取到了锁"); | ||
lock.unlock(); | ||
} | ||
|
||
public void method2() throws InterruptedException { | ||
lock.lock(); | ||
Thread.sleep(5000); | ||
System.out.println("method2 释放锁"); | ||
lock.unlock(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package thread_demo.demo43; | ||
|
||
public class MyThread extends Thread { | ||
|
||
private MyService service; | ||
|
||
public MyThread(MyService service) { | ||
this.service = service; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
service.method1(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package thread_demo.demo43; | ||
|
||
/** | ||
*公平锁与非公平锁 : | ||
* 公平锁 : 线程获取锁的顺序是按照线程加载锁的顺序来的(FIFO的顺序) | ||
* 非公平锁 : 线程获取锁的顺序是随机的,和线程加载锁的顺序没有关系 | ||
* | ||
* new ReentrantLock(true) : 公平锁; new ReentrantLock(false) : 非公平锁 | ||
*/ | ||
public class Run { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
|
||
final MyService service = new MyService(true); | ||
//TODO | ||
//好像都是公平锁,为什么没有效果? | ||
// final MyService service = new MyService(false); | ||
|
||
Thread[] threads = new Thread[40]; | ||
for (int i = 0; i < threads.length; i++) { | ||
threads[i] = new MyThread(service); | ||
} | ||
|
||
new Thread() { | ||
@Override | ||
public void run() { | ||
try { | ||
service.method2(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}.start(); | ||
|
||
Thread.sleep(100); | ||
|
||
for (int i = 0; i < threads.length; i++) { | ||
threads[i].start(); | ||
Thread.sleep(100); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters