我试图了解ReentrantLock如何在java中工作.
让我们考虑一个简单的例子如下:
private ReentrantLock lock;
public void foo() {
lock.lock();
try{
...
}finally {
lock.unlock();
}
}
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚lock()方法的调用层次结构.
public void lock() {
sync.lock();
}
Run Code Online (Sandbox Code Playgroud)
对于FairSync:
final void lock() {
acquire(1);
}
Run Code Online (Sandbox Code Playgroud)
对于NonFairSync:
final void lock() {
if (compareAndSetState(0, 1))
setExclusiveOwnerThread(Thread.currentThread());
else
acquire(1);
}
Run Code Online (Sandbox Code Playgroud)
两个lock()方法都调用参数为1的acquire()方法.
在AbstractQueuedSynchronizer类中:
public final void acquire(int arg) {
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}
static void selfInterrupt() {
Thread.currentThread().interrupt();
}
Run Code Online (Sandbox Code Playgroud)
如果当前线程无法获取资源(即某个其他线程已获取此资源),则当前线程必须等待.在这种情况下,ReentrantLock调用selfInterrupt()方法.
现在我的问题是interrupt()方法如何能够停止一个等同于synchronized中的wait()方法的线程?
另外,在资源被另一个线程释放后,currentThread如何自动启动?(在内部调用的另一个线程调用unlock()方法之后sync.release(1);)
我也试图找出如何中断()方法的工作原理在这里却找不到答案,我的问题.
我有两个带有mysql的grails表
说A和B.
我想在这里实现的场景是:
(1)A的实例可以有零个/一个/多个B实例.
(2)当删除实例A时,必须删除其所有相关的B.
(3)B的每个实例必须只与A的一个实例相关联.
(4)A知道B,但B不知道A.
条件号4不是强制性的.
从上面的信息看起来像:从A到B的单向一对多
目前我在做的是:(从这里获得帮助)
class A
{
String name
Set bs=[]
static hasMany=[bs:B]
}
Class B
{
String name
}
B b=new B(name:'bname')
b.save()
A a=new A(name:'aname')
a.addToBs(b)
a.save()
Run Code Online (Sandbox Code Playgroud)
保存两个条目时,(B正在保存,但A未保存)我使用addTo并获取错误没有方法addToBs()的签名
如果我错了,请帮助我并纠正我.
肥皂和休息之间有什么区别
在设置一些新的Web服务或集成环境时,会多次提出此问题.哪一个更好.
我知道一些基本的区别,如下所述:
(1)Rest is based on http protocol (get,put,post,delete) , treating everything as a resource.
whereas SOAP is transport agnostic
(2)Soap works only with xml , Rest works with json/xml both.
(3)rest do not provide schema definition when implemented as json,
Soap will always provide schema definitions.
It will be easy to understand request/response schema and
their data type constraints when viewing schema information via WSDL by any client.
(4)The step of wsdl creation makes it difficult to make …Run Code Online (Sandbox Code Playgroud) 我正在尝试了解 ThreadPoolExecutor 类。我发现该类中声明了一些最终变量,但无法理解它们的用途。
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
private static final int COUNT_BITS = Integer.SIZE - 3; //29
private static final int CAPACITY = (1 << COUNT_BITS) - 1; //536870911 00011111111111111111111111111111
// RUN_STATE is stored in the high-order bits
private static final int RUNNING = -1 << COUNT_BITS; //-536870912 11100000000000000000000000000000
private static final int SHUTDOWN = 0 << COUNT_BITS; //0 00000000000000000000000000000000
private static final int STOP = 1 << COUNT_BITS; //536870912 00100000000000000000000000000000
private static final int TIDYING …Run Code Online (Sandbox Code Playgroud) 我是hibernate的新手.我尝试了一些使用hibernate API的基本CRUD操作.
我创建了一个Person类
@Entity
class Person
{
String name
}
Run Code Online (Sandbox Code Playgroud)
我能够在数据库中保存这个类.直到现在我才认为JPA内部使这个类实现Serializable,因为只有序列化才能保存对象的状态.但是我试过这个:
Person p=new Person();
boolean bool=p instanceof Serializable;
sop(bool); //false
Run Code Online (Sandbox Code Playgroud)
然后我创建了另一个类Human(在Hibernate doc上找到了这种实现方式)
@Entity
class Human implements Serializable
{
String name
}
Human h=new Human();
boolean bool=h instanceof Serializable
sop(bool); //true
Run Code Online (Sandbox Code Playgroud)
我们应该以哪种方式创建我们的域类以及hibernate如何在内部处理这两种方式?
请帮忙.
我试着理解春天了.我读了这个春天的文件.它表示方面的构造函数运行两次,一次用于原始类对象,另一次用于代理对象.但是当我尝试使用公共构造函数创建自己的方面时,它只执行一次.
以下是我的代码片段:
package com.akash.aop;
public class TestAopBefore {
public TestAopBefore() {
System.out.println("TestAopBefore.TestAopBefore()");
}
private String name;
public String getName() {
System.out.println("TestAopBefore.getName()");
return name;
}
public void setName(String name) {
System.out.println("TestAopBefore.setName()");
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
我的Aspect类
package com.akash.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MyAspect {
@Before("execution(* com.akash.aop.TestAopBefore.get*())")
public void runBeforeAllGetter() {
System.out.println("MyAspect.runBeforeAllGetter()");
}
}
Run Code Online (Sandbox Code Playgroud)
我的xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="testAopBefore" class="com.akash.aop.TestAopBefore" />
<bean id="myAspect" class="com.akash.aop.MyAspect" />
<aop:aspectj-autoproxy proxy-target-class="true" />
</beans>
Run Code Online (Sandbox Code Playgroud)
我的App类 …
java ×5
spring ×2
grails ×1
grails-orm ×1
hibernate ×1
jpa ×1
rest ×1
soap ×1
spring-aop ×1
web-services ×1