📄 testsearchlock.java
字号:
/*
* 创建日期 Mar 21, 2006
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package net.nutch.tools;
import org.apache.log4j.PropertyConfigurator;
import net.nutch.searcher.SearcherLock;
public class TestSearchLock {
private SearcherLock searcherLock = new SearcherLock();
private boolean getLock = true;
private static String GETLOCK = "GETLOCK";
public void testLock() throws Exception{
int lock = searcherLock.getLock();
int times = 0;
while(lock < 0){
System.out.println("Can't get a lock!");
times ++;
if (times > 5){
System.out.println("test " + times + " times, Can't get a lock!");
return;
}
Thread.sleep(2000);
lock = searcherLock.getLock();
}
Thread.sleep(2000);
searcherLock.unLock(lock);
}
public void testAllUnLock() throws Exception{
searcherLock.stop();
int times = 0;
while (!searcherLock.isAllUnlock()) {
System.out.println("Not all locks are unlock!");
times ++;
if (times > 5){
searcherLock.start();
System.out.println("test " + times + " times, Not all locks are free!");
return;
}
Thread.sleep(2000);
}
System.out.println("All locks are free!!!");
Thread.sleep(2000);
searcherLock.start();
}
class TestLock extends Thread {
public void run(){
while(true){
try{
synchronized(GETLOCK){
if (!getLock){
continue;
}
}
testLock();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
class TestAllUnLock extends Thread {
public void run(){
while(true){
try{
synchronized(GETLOCK){
getLock = false;
}
testAllUnLock();
synchronized(GETLOCK){
getLock = true;
}
sleep(10000);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public void test(){
TestLock t1 = new TestLock();
t1.start();
TestLock t2 = new TestLock();
t2.start();
TestLock t3 = new TestLock();
t3.start();
TestLock t4 = new TestLock();
t4.start();
TestLock t5 = new TestLock();
t5.start();
TestLock t6 = new TestLock();
t6.start();
TestAllUnLock t7 = new TestAllUnLock();
t7.start();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
PropertyConfigurator.configure("log4j.properties");
TestSearchLock test = new TestSearchLock();
test.test();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -