Skip to content

Commit

Permalink
fix ExecutingRunnable hashcode (rabbitmq#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
alaneuler authored Jun 25, 2021
1 parent a494c00 commit 430956c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.alipay.sofa.common.thread;

import java.util.Objects;

/**
* The wrapper to the {@link Runnable} to save it's execute {@link Thread}
* @author huzijie
Expand All @@ -35,16 +37,25 @@ class ExecutingRunnable implements Runnable {

private volatile boolean printed;

private Integer hashCode;

public ExecutingRunnable(Runnable originRunnable) {
if (originRunnable == null) {
throw new NullPointerException();
}
this.originRunnable = originRunnable;
}

// ExecutingRunnable won't be executed by more than one thread
@Override
public int hashCode() {
return toString().hashCode();
if (hashCode == null) {
// Save hashcode for later retrieval
// ExecutingRunnable is saved in a map, we may fail to remove if this changes
hashCode = Objects.hash(originRunnable, thread);
}

return hashCode;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public void cleanExecutor() throws InterruptedException {
}
}

@Test
public void testThreadNameChanged() throws InterruptedException {
int old = threadPool.getStatistics().getExecutingTasks().size();
threadPool.execute(() -> Thread.currentThread().setName("test-name"));
Thread.sleep(1000);
Assert.assertEquals(old, threadPool.getStatistics().getExecutingTasks().size());
}

@Test
public void testDecayedTask() throws Exception {
Assert.assertTrue(isMatch(getInfoViaIndex(0), INFO, String.format(
Expand Down

0 comments on commit 430956c

Please sign in to comment.