Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add thread context/implementations for managing threads in Raft server.
- Loading branch information
Showing
17 changed files
with
560 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
core/src/main/java/io/atomix/util/concurrent/AtomixThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,56 @@ | |||
/* | |||
* Copyright 2015 the original author or authors. | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
package io.atomix.util.concurrent; | |||
|
|||
import java.lang.ref.WeakReference; | |||
|
|||
/** | |||
* Atomix thread. | |||
* <p> | |||
* The Atomix thread primarily serves to store a {@link ThreadContext} for the current thread. | |||
* The context is stored in a {@link WeakReference} in order to allow the thread to be garbage collected. | |||
* <p> | |||
* There is no {@link ThreadContext} associated with the thread when it is first created. | |||
* It is the responsibility of thread creators to {@link #setContext(ThreadContext) set} the thread context when appropriate. | |||
* | |||
* @author <a href="http://github.com/kuujo">Jordan Halterman</a> | |||
*/ | |||
public class AtomixThread extends Thread { | |||
private WeakReference<ThreadContext> context; | |||
|
|||
public AtomixThread(Runnable target, String name) { | |||
super(target, name); | |||
} | |||
|
|||
/** | |||
* Sets the thread context. | |||
* | |||
* @param context The thread context. | |||
*/ | |||
public void setContext(ThreadContext context) { | |||
this.context = new WeakReference<>(context); | |||
} | |||
|
|||
/** | |||
* Returns the thread context. | |||
* | |||
* @return The thread {@link ThreadContext} or {@code null} if no context has been configured. | |||
*/ | |||
public ThreadContext getContext() { | |||
return context != null ? context.get() : null; | |||
} | |||
|
|||
} |
43 changes: 43 additions & 0 deletions
43
core/src/main/java/io/atomix/util/concurrent/AtomixThreadFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,43 @@ | |||
/* | |||
* Copyright 2015 the original author or authors. | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
package io.atomix.util.concurrent; | |||
|
|||
import java.util.concurrent.ThreadFactory; | |||
import java.util.concurrent.atomic.AtomicInteger; | |||
|
|||
/** | |||
* Named thread factory. | |||
* | |||
* @author <a href="http://github.com/kuujo">Jordan Halterman</a> | |||
*/ | |||
public class AtomixThreadFactory implements ThreadFactory { | |||
private final AtomicInteger threadNumber = new AtomicInteger(1); | |||
private final String nameFormat; | |||
|
|||
/** | |||
* Creates a thread factory that names threads according to the {@code nameFormat} by supplying a | |||
* single argument to the format representing the thread number. | |||
*/ | |||
public AtomixThreadFactory(String nameFormat) { | |||
this.nameFormat = nameFormat; | |||
} | |||
|
|||
@Override | |||
public Thread newThread(Runnable r) { | |||
return new AtomixThread(r, String.format(nameFormat, threadNumber.getAndIncrement())); | |||
} | |||
|
|||
} |
31 changes: 31 additions & 0 deletions
31
core/src/main/java/io/atomix/util/concurrent/Scheduled.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,31 @@ | |||
/* | |||
* Copyright 2015 the original author or authors. | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
|
|||
package io.atomix.util.concurrent; | |||
|
|||
/** | |||
* Scheduled task. | |||
* | |||
* @author <a href="http://github.com/kuujo>Jordan Halterman</a> | |||
*/ | |||
public interface Scheduled { | |||
|
|||
/** | |||
* Cancels the scheduled task. | |||
*/ | |||
void cancel(); | |||
|
|||
} |
Oops, something went wrong.