Skip to content

Commit

Permalink
DefaultThreadFactory: using AtomicInteger to number thread rather tha…
Browse files Browse the repository at this point in the history
…n synchronized block around incrementing a short (less locking, and less number overflowing)
  • Loading branch information
belaban committed Dec 19, 2024
1 parent b6e06fa commit 451ee4d
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions src/org/jgroups/util/DefaultThreadFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.jgroups.logging.Log;

import java.util.concurrent.atomic.AtomicInteger;

/**
* Thread factory mainly responsible for naming of threads. Can be replaced by
* user. If use_numbering is set, a thread THREAD will be called THREAD-1,
Expand All @@ -15,24 +17,24 @@
* @author Bela Ban
*/
public class DefaultThreadFactory implements ThreadFactory {
protected final String baseName;
protected final boolean createDaemons;
protected final boolean use_numbering;
protected short counter; // if numbering is enabled
protected boolean includeClusterName;
protected String clusterName;
protected boolean includeLocalAddress;
protected String address;
protected boolean use_vthreads; // use fibers instead of threads (requires Java 15)
protected Log log;


public DefaultThreadFactory(String baseName, boolean createDaemons) {
this(baseName, createDaemons, false);
protected final String baseName;
protected final boolean createDaemons;
protected final boolean use_numbering;
protected final AtomicInteger counter=new AtomicInteger(); // if numbering is enabled
protected boolean includeClusterName;
protected String clusterName;
protected boolean includeLocalAddress;
protected String address;
protected boolean use_vthreads; // use fibers instead of threads (requires Java 15)
protected Log log;


public DefaultThreadFactory(String base_name, boolean createDaemons) {
this(base_name, createDaemons, false);
}

public DefaultThreadFactory(String baseName, boolean createDaemons, boolean use_numbering) {
this.baseName=baseName;
public DefaultThreadFactory(String base_name, boolean createDaemons, boolean use_numbering) {
this.baseName=base_name;
this.createDaemons=createDaemons;
this.use_numbering=use_numbering;
}
Expand Down Expand Up @@ -107,10 +109,7 @@ protected String getThreadName(String base_name, final Thread thread, String add
return null;
StringBuilder sb=new StringBuilder(base_name != null? base_name : thread.getName());
if(use_numbering) {
short id;
synchronized(this) {
id=++counter;
}
int id=counter.incrementAndGet();
sb.append("-").append(id);
}

Expand All @@ -136,10 +135,7 @@ protected String getThreadName(String base_name, final Thread thread, String add
protected String getNewThreadName(String base_name, String addr, String cluster_name) {
StringBuilder sb=new StringBuilder(base_name != null? base_name : "thread");
if(use_numbering) {
short id;
synchronized(this) {
id=++counter;
}
int id=counter.incrementAndGet();
sb.append("-").append(id);
}

Expand Down

0 comments on commit 451ee4d

Please sign in to comment.