Skip to content

Commit

Permalink
reproduce 6153
Browse files Browse the repository at this point in the history
  • Loading branch information
zsxwing committed Jan 6, 2017
1 parent a8950df commit 36522e7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
18 changes: 15 additions & 3 deletions common/src/main/java/io/netty/util/Recycler.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public void recycle(Object object) {
private static final int DEFAULT_INITIAL_MAX_CAPACITY_PER_THREAD = 32768; // Use 32k instances as default.
private static final int DEFAULT_MAX_CAPACITY_PER_THREAD;
private static final int INITIAL_CAPACITY;
private static final int MAX_SHARED_CAPACITY_FACTOR;
private static final int MAX_DELAYED_QUEUES_PER_THREAD;
static final int MAX_SHARED_CAPACITY_FACTOR;
static final int MAX_DELAYED_QUEUES_PER_THREAD;
private static final int LINK_CAPACITY;
private static final int RATIO;

Expand Down Expand Up @@ -358,6 +358,7 @@ boolean transfer(Stack<?> dst) {

if (dst.dropHandle(element)) {
// Drop the object.
System.out.println("drop handle!!!! this: " + this + " handle: " + element);
continue;
}
element.stack = dst;
Expand Down Expand Up @@ -490,13 +491,22 @@ boolean scavengeSome() {
boolean success = false;
WeakOrderQueue prev = this.prev;
do {
System.out.println("loop prev: " + prev + " cursor: " + cursor);
if (cursor.transfer(this)) {
success = true;
break;
}

try {
// Sleep a while to wait for adding new item to cursor
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.gc();
System.out.println("gc end!!!!");
WeakOrderQueue next = cursor.next;
if (cursor.owner.get() == null) {
System.out.println("thread dead, WeakOrderQueue: " + cursor);
// If the thread associated with the queue is gone, unlink it, after
// performing a volatile read to confirm there is no data left to collect.
// We never unlink the first queue, as we don't want to synchronize on updating the head.
Expand All @@ -522,6 +532,7 @@ boolean scavengeSome() {

this.prev = prev;
this.cursor = cursor;
System.out.println("leave prev: " + prev + " cursor: " + cursor + " success: " + success);
return success;
}

Expand Down Expand Up @@ -579,6 +590,7 @@ private void pushLater(DefaultHandle<?> item, Thread thread) {
return;
}

System.out.println("adding : " + queue + " item: " + item);
queue.add(item);
}

Expand Down
47 changes: 46 additions & 1 deletion common/src/test/java/io/netty/util/RecyclerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class RecyclerTest {

private static Recycler<HandledObject> newRecycler(int max) {
return new Recycler<HandledObject>(max) {
return new Recycler<HandledObject>(max, Recycler.MAX_SHARED_CAPACITY_FACTOR, 0, Recycler.MAX_DELAYED_QUEUES_PER_THREAD) {
@Override
protected HandledObject newObject(
Recycler.Handle<HandledObject> handle) {
Expand Down Expand Up @@ -217,4 +217,49 @@ void recycle() {
handle.recycle(this);
}
}

private Thread recycleInNewThread(final HandledObject object, final HandledObject object2) throws InterruptedException {
final Thread thread = new Thread() {
@Override
public void run() {
object.recycle();
System.out.println(object + " was recycled");
if (object2 != null) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
object2.recycle();
System.out.println(object2 + " was recycled");
}
}
};
thread.start();
if (object2 == null) {
thread.join();
}
return thread;
}

@Test
public void test6153() throws Exception {
final Recycler<HandledObject> recycler = newRecycler(1024);
HandledObject object1 = recycler.get();
HandledObject object2 = recycler.get();
HandledObject object3 = recycler.get();
HandledObject object4 = recycler.get();

Thread t1 = recycleInNewThread(object1, object4);
Thread.sleep(100);
Thread t2 = recycleInNewThread(object2, null);
Thread t3 = recycleInNewThread(object3, null);
assertEquals(object3, recycler.get());
assertEquals(object2, recycler.get());
assertEquals(object1, recycler.get());
t1 = null;
assertEquals(object4, recycler.get());
t3 = null;
recycler.get();
}
}

0 comments on commit 36522e7

Please sign in to comment.