本文整理匯總了Java中java.lang.Thread類的典型用法代碼示例。如果您正苦於以下問題:Java Thread類的具體用法?Java Thread怎麽用?Java Thread使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Thread類屬於java.lang包,在下文中一共展示了Thread類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import java.lang.Thread; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
DisplayChangeVITest test = new DisplayChangeVITest();
GraphicsDevice gd =
GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice();
if (gd.isFullScreenSupported()) {
gd.setFullScreenWindow(test);
Thread t = new Thread(test);
t.run();
synchronized (lock) {
while (!done) {
try {
lock.wait(50);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
System.err.println("Test Passed.");
} else {
System.err.println("Full screen not supported. Test passed.");
}
}
示例2: finish
import java.lang.Thread; //導入依賴的package包/類
private void finish(IORobot bot1, IORobot bot2) throws InterruptedException
{
bot1.finish();
Thread.sleep(200);
bot2.finish();
Thread.sleep(200);
Thread.sleep(200);
// write everything
// String outputFile = this.writeOutputFile(this.gameId, this.engine.winningPlayer());
this.saveGame(bot1, bot2);
System.exit(0);
}
示例3: stopPlayout
import java.lang.Thread; //導入依賴的package包/類
private boolean stopPlayout() {
Logging.d(TAG, "stopPlayout");
assertTrue(audioThread != null);
logUnderrunCount();
audioThread.stopThread();
final Thread aThread = audioThread;
audioThread = null;
if (aThread != null) {
Logging.d(TAG, "Stopping the AudioTrackThread...");
aThread.interrupt();
if (!ThreadUtils.joinUninterruptibly(aThread, AUDIO_TRACK_THREAD_JOIN_TIMEOUT_MS)) {
Logging.e(TAG, "Join of AudioTrackThread timed out.");
}
Logging.d(TAG, "AudioTrackThread has now been stopped.");
}
releaseAudioResources();
return true;
}
示例4: delToken
import java.lang.Thread; //導入依賴的package包/類
private void delToken(final CallbackContext callbackContext) {
new Thread() {
@Override
public void run() {
try {
if (token != "" && null != huaweiApiClient) {
HuaweiPush.HuaweiPushApi.deleteToken(huaweiApiClient, token);
callbackContext.success();
} else {
Log.w(TAG, "delete token's params is invalid.");
callbackContext.error("token not exists");
}
} catch (Exception e) {
callbackContext.error("error occered when delete token");
Log.e("PushLog", "delete token exception, " + e.toString());
}
}
}.start();
}
示例5: errObj
import java.lang.Thread; //導入依賴的package包/類
private WritableMap errObj(final String code, final String message) {
WritableMap err = Arguments.createMap();
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
String stackTraceString = "";
for (StackTraceElement e : stackTrace) {
stackTraceString += e.toString() + "\n";
}
err.putString("err", code);
err.putString("message", message);
err.putString("stackTrace", stackTraceString);
Log.e(LOG_TAG, message);
Log.d(LOG_TAG, stackTraceString);
return err;
}
示例6: errObj
import java.lang.Thread; //導入依賴的package包/類
private WritableMap errObj(final String code, final String message, final boolean enableLog) {
WritableMap err = Arguments.createMap();
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
String stackTraceString = "";
for (StackTraceElement e : stackTrace) {
stackTraceString += e.toString() + "\n";
}
err.putString("err", code);
err.putString("message", message);
if (enableLog) {
err.putString("stackTrace", stackTraceString);
Log.e(LOG_TAG, message);
Log.d(LOG_TAG, stackTraceString);
}
return err;
}
示例7: callingOnStartCommandMultipleTimes
import java.lang.Thread; //導入依賴的package包/類
/**
* Test that starting the service is idempotent (only the first one counts).
*/
@Test
public void callingOnStartCommandMultipleTimes() throws Exception {
// Sleep briefly to avoid racing with the first background task.
Thread.sleep(AVOID_BACKGROUND_TASK_RACE_SLEEP_TIME);
Date startTime = service.getServiceStartTime();
int backgroundRunCount = service.getBackgroundTasksRunCount();
Intent intent = new Intent(Robolectric.application, RangzenService.class);
service.onStartCommand(intent, 0, startId);
startId++;
assertEquals("Start time changed calling onStartCommand again",
startTime, service.getServiceStartTime());
assertEquals("Background run count changed calling onStartCommand again",
backgroundRunCount, service.getBackgroundTasksRunCount());
}
示例8: setClipboardTextAndClean
import java.lang.Thread; //導入依賴的package包/類
public void setClipboardTextAndClean(String text) {
Log.i("netclip", "Clipboard.setClipboardText - " + text);
this.stopCurrentCleanThread();
this.continueCleanThread = true;
this.androidClipboard.setPrimaryClip(
ClipData.newPlainText("netclip", text)
);
this.currentCleanRunnable = new ClipboardCleanRunnable();
this.currentCleanThread = new Thread(this.currentCleanRunnable);
Log.i("netclip", "Clipboard.setClipboardText - starting new clipboard clean thread");
this.currentCleanThread.start();
Message message = this.handler.obtainMessage();
message.obj = "Copied to clipboard";
message.sendToTarget();
}
示例9: run
import java.lang.Thread; //導入依賴的package包/類
@Override
public void run() {
Log.i("netclip", "ClipboardCleanRunnable.run");
int k = 0;
while (k <= 15 * 10 && this.replaced == false) {
Log.i("netclip", "ClipboardCleanRunnable.run - iteration");
try {
Thread.sleep(100);
k++;
} catch (InterruptedException ex) { }
}
if (this.replaced == false) {
Log.i("netclip", "ClipboardCleanRunnable.run - cleaning clipboard");
Clipboard.getInstance(null).reset();
}
Log.i("netclip", "ClipboardCleanRunnable.run - finished execution");
}
示例10: interruptIngestorThread
import java.lang.Thread; //導入依賴的package包/類
private boolean interruptIngestorThread(String collectionId)
{
ConcurrentHashMap<String, Thread> ingestorThreads = null;
try {
ingestorThreads = this.getIngestorThreads();
} catch (Exception e) {
return false;
}
if(ingestorThreads.containsKey(collectionId))
{
// That means it is still alive
Thread ingestorThread = ingestorThreads.get(collectionId);
if(ingestorThread.isAlive())
{
ingestorThread.interrupt();
return true;
}
}
return false;
}
示例11: interruptAllIngestorThreads
import java.lang.Thread; //導入依賴的package包/類
public void interruptAllIngestorThreads()
{
try
{
for(Thread ingestorThread: this.getIngestorThreads().values())
{
if(ingestorThread.isAlive())
{
ingestorThread.interrupt();
}
}
if(this.reharvesterThread!=null && this.reharvesterThread.isAlive())
this.reharvesterThread.interrupt();
// Give the threads five seconds to try to clean themselves up
Thread.sleep(5000);
}
catch (Exception e1) {}
}
示例12: main
import java.lang.Thread; //導入依賴的package包/類
public static void main(String args[]){
Game g = new Game(20,40,1);
Input in = new Input();
in.start();
while(g.play){
try{
g.refresh();
if(in.getState() != Thread.State.WAITING){
g.b.y--;
}else{g.b.y++;}
g.count++;
Thread.sleep(1000/g.fps); //inverse of refresh rate
if(g.count > 5){
throw new Exception();
}
}catch(Exception e){
System.out.println("Game over");
g.play = false;
}
}
}
示例13: spawnProcess
import java.lang.Thread; //導入依賴的package包/類
/**
* Spawn an OS subprocess. Follows the Oracle-recommended method of
* using ProcessBuilder rather than Runtime.exec(), and includes some
* safeguards to prevent blocked and stale processes. A thread is
* created that will perform the spawn, consume its output and error
* streams (to prevent blocking due to full buffers), then clean up.
*
* @param cmd The cmd/arg list for execution
*/
static public void spawnProcess(final List<String> cmd) {
if (cmd == null)
return;
Thread t = new Thread() {
public void run() {
Process proc = null;
proc = startProcess(cmd, true);
if (proc != null) {
consumeProcessOutput(proc);
try {
proc.waitFor();
}
catch (InterruptedException e) {
// ignore (we terminate anyway)
}
destroyProcess(proc);
}
Thread.currentThread().interrupt();
}
};
t.setDaemon(true);
t.start();
}
示例14: FifoTaskScheduler
import java.lang.Thread; //導入依賴的package包/類
public FifoTaskScheduler(int max, NodeMonitor nodeMonitor) {
maxActiveTasks = max;
activeTasks = 0;
activeBigTasks = 0;
activeBigTasksRequestId = new HashSet<String>();
bigTaskReservations = 0;
this.nodeMonitor = nodeMonitor;
this.stealing = nodeMonitor.getStealing();
new Thread(""){
public void run(){
while(true){
try{
Thread.sleep(1000);
}catch(Exception e){}
LOG.info("Currently executing: "+activeTasks+" big: "+activeBigTasks+" queue: "+taskReservations.size()+ " big enqueued: "+ bigTaskReservations);
}
}
}.start();
}
示例15: instantiateIngest
import java.lang.Thread; //導入依賴的package包/類
/**
* Instantiates ingest thread for the current edits segment.
*/
private void instantiateIngest() throws IOException {
InjectionHandler.processEvent(InjectionEvent.STANDBY_INSTANTIATE_INGEST);
try {
synchronized (ingestStateLock) {
if (checkIngestState()) {
LOG.info("Standby: Ingest for txid: " + currentSegmentTxId
+ " is already running");
return;
}
assertState(StandbyIngestState.NOT_INGESTING);
ingest = new Ingest(this, fsnamesys, confg, currentSegmentTxId);
ingestThread = new Thread(ingest);
ingestThread.setName("Ingest_for_" + currentSegmentTxId);
ingestThread.start();
currentIngestState = StandbyIngestState.INGESTING_EDITS;
}
LOG.info("Standby: Instatiated ingest for txid: " + currentSegmentTxId);
} catch (IOException e) {
setIngestFailures(ingestFailures + 1);
currentIngestState = StandbyIngestState.NOT_INGESTING;
throw e;
}
}