Example usage for java.lang Thread interrupted

List of usage examples for java.lang Thread interrupted

Introduction

In this page you can find the example usage for java.lang Thread interrupted.

Prototype

public static boolean interrupted() 

Source Link

Document

Tests whether the current thread has been interrupted.

Usage

From source file:org.apache.tez.runtime.LogicalIOProcessorRuntimeTask.java

public void close() throws Exception {
    try {//from ww  w  .  j a va 2s  .  c  o m
        Preconditions.checkState(this.state.get() == State.RUNNING,
                "Can only run while in RUNNING state. Current: " + this.state);
        this.state.set(State.CLOSED);

        List<List<Event>> allCloseInputEvents = Lists.newArrayList();
        // Close the Inputs.
        for (InputSpec inputSpec : inputSpecs) {
            String srcVertexName = inputSpec.getSourceVertexName();
            initializedInputs.remove(srcVertexName);
            List<Event> closeInputEvents = ((InputFrameworkInterface) inputsMap.get(srcVertexName)).close();
            allCloseInputEvents.add(closeInputEvents);
        }

        List<List<Event>> allCloseOutputEvents = Lists.newArrayList();
        // Close the Outputs.
        for (OutputSpec outputSpec : outputSpecs) {
            String destVertexName = outputSpec.getDestinationVertexName();
            initializedOutputs.remove(destVertexName);
            List<Event> closeOutputEvents = ((LogicalOutputFrameworkInterface) outputsMap.get(destVertexName))
                    .close();
            allCloseOutputEvents.add(closeOutputEvents);
        }

        // Close the Processor.
        processorClosed = true;
        processor.close();

        for (int i = 0; i < allCloseInputEvents.size(); i++) {
            String srcVertexName = inputSpecs.get(i).getSourceVertexName();
            sendTaskGeneratedEvents(allCloseInputEvents.get(i), EventProducerConsumerType.INPUT,
                    taskSpec.getVertexName(), srcVertexName, taskSpec.getTaskAttemptID());
        }

        for (int i = 0; i < allCloseOutputEvents.size(); i++) {
            String destVertexName = outputSpecs.get(i).getDestinationVertexName();
            sendTaskGeneratedEvents(allCloseOutputEvents.get(i), EventProducerConsumerType.OUTPUT,
                    taskSpec.getVertexName(), destVertexName, taskSpec.getTaskAttemptID());
        }

    } finally {
        setTaskDone();
        // Clear the interrupt status since the task execution is done.
        Thread.interrupted();
        if (eventRouterThread != null) {
            eventRouterThread.interrupt();
            LOG.info("Joining on EventRouter");
            try {
                eventRouterThread.join();
            } catch (InterruptedException e) {
                LOG.info("Ignoring interrupt while waiting for the router thread to die");
                Thread.currentThread().interrupt();
            }
            eventRouterThread = null;
        }
        String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
        System.err.println(
                timeStamp + " Completed running task attempt: " + taskSpec.getTaskAttemptID().toString());
        System.out.println(
                timeStamp + " Completed running task attempt: " + taskSpec.getTaskAttemptID().toString());
    }
}

From source file:LinkedTransferQueue.java

public void transfer(E e) throws InterruptedException {
    if (e == null) {
        throw new NullPointerException();
    }//  www.j  a va  2 s  . c  o  m
    if (xfer(e, WAIT, 0) == null) {
        Thread.interrupted();
        throw new InterruptedException();
    }
}

From source file:com.yahoo.ads.pb.kafka.KafkaSimpleConsumer.java

public long getLastOffset() throws InterruptedException {
    OffsetResponse response = null;//w  w w . j  a v a2  s .c  o m
    Broker previousLeader = leaderBroker;
    while (true) {
        TopicAndPartition topicAndPartition = new TopicAndPartition(topic, partitionId);
        Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
        requestInfo.put(topicAndPartition,
                new PartitionOffsetRequestInfo(kafka.api.OffsetRequest.LatestTime(), 1));
        kafka.javaapi.OffsetRequest request = new kafka.javaapi.OffsetRequest(requestInfo,
                kafka.api.OffsetRequest.CurrentVersion(), clientId);

        ensureConsumer(previousLeader);
        try {
            response = consumer.getOffsetsBefore(request);
        } catch (Exception e) {
            // e could be an instance of ClosedByInterruptException as SimpleConsumer.fetch uses nio
            if (Thread.interrupted()) {
                logger.info("catch exception of {} with interrupted in getLastOffset for {} - {}",
                        e.getClass().getName(), topic, partitionId);

                throw new InterruptedException();
            }
            logger.warn("caughte exception in getLastOffset {} - {}", topic, partitionId, e);
            response = null;
        }
        if (response == null || response.hasError()) {
            short errorCode = response != null ? response.errorCode(topic, partitionId)
                    : ErrorMapping.UnknownCode();

            logger.warn("Error fetching data Offset for {} - {}, the Broker. Reason: {}", topic, partitionId,
                    errorCode);

            stopConsumer();
            previousLeader = leaderBroker;
            leaderBroker = null;
            continue;
        }
        break;
    }
    long[] offsets = response.offsets(topic, partitionId);
    return offsets[offsets.length - 1];
}

From source file:org.apache.samza.coordinator.AzureJobCoordinator.java

/**
 * Called when the JC detects a job model version upgrade on the shared blob.
 * @param nextJMVersion The new job model version after rebalancing.
 *//*ww w  .j  a v a 2s. c o m*/
private void onNewJobModelAvailable(final String nextJMVersion) {
    LOG.info("pid=" + processorId + "new JobModel available with job model version {}", nextJMVersion);

    //Get the new job model from blob
    jobModel = leaderBlob.getJobModel();
    LOG.info("pid=" + processorId + ": new JobModel available. ver=" + nextJMVersion + "; jm = " + jobModel);

    if (!jobModel.getContainers().containsKey(processorId)) {
        LOG.info("JobModel: {} does not contain the processorId: {}. Stopping the processor.", jobModel,
                processorId);
        stop();
        table.deleteProcessorEntity(currentJMVersion.get(), processorId);
    } else {
        //Stop current work
        if (coordinatorListener != null) {
            coordinatorListener.onJobModelExpired();
        }
        // Add entry with new job model version to the processor table
        table.addProcessorEntity(nextJMVersion, processorId, azureLeaderElector.amILeader());

        // Start polling blob to check if barrier reached
        Random random = new Random();
        String blobBarrierState = leaderBlob.getBarrierState();
        while (true) {
            if (blobBarrierState.equals(BarrierState.END.name() + " " + nextJMVersion)) {
                LOG.info("Barrier completion detected by the worker for barrier version {}.", nextJMVersion);
                versionUpgradeDetected.getAndSet(false);
                onNewJobModelConfirmed(nextJMVersion);
                break;
            } else if (blobBarrierState.equals(BarrierState.TIMEOUT.name() + " " + nextJMVersion)
                    || (Integer.valueOf(leaderBlob.getJobModelVersion()) > Integer.valueOf(nextJMVersion))) {
                LOG.info("Barrier timed out for version number {}", nextJMVersion);
                versionUpgradeDetected.getAndSet(false);
                break;
            } else {
                try {
                    Thread.sleep(random.nextInt(5000));
                } catch (InterruptedException e) {
                    Thread.interrupted();
                }
                LOG.info("Checking for barrier state on the blob again...");
                blobBarrierState = leaderBlob.getBarrierState();
            }
        }
    }
}

From source file:org.apache.hadoop.mapreduce.v2.app.job.impl.TestJobImpl.java

@Test(timeout = 20000)
public void testKilledDuringFailAbort() throws Exception {
    Configuration conf = new Configuration();
    conf.set(MRJobConfig.MR_AM_STAGING_DIR, stagingDir);
    AsyncDispatcher dispatcher = new AsyncDispatcher();
    dispatcher.init(conf);//from  w ww .j av a 2 s  . c  o m
    dispatcher.start();
    OutputCommitter committer = new StubbedOutputCommitter() {
        @Override
        public void setupJob(JobContext jobContext) throws IOException {
            throw new IOException("forced failure");
        }

        @Override
        public synchronized void abortJob(JobContext jobContext, State state) throws IOException {
            while (!Thread.interrupted()) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
        }
    };
    CommitterEventHandler commitHandler = createCommitterEventHandler(dispatcher, committer);
    commitHandler.init(conf);
    commitHandler.start();

    JobImpl job = createStubbedJob(conf, dispatcher, 2, null);
    JobId jobId = job.getID();
    job.handle(new JobEvent(jobId, JobEventType.JOB_INIT));
    assertJobState(job, JobStateInternal.INITED);
    job.handle(new JobStartEvent(jobId));
    assertJobState(job, JobStateInternal.FAIL_ABORT);

    job.handle(new JobEvent(jobId, JobEventType.JOB_KILL));
    assertJobState(job, JobStateInternal.KILLED);
    dispatcher.stop();
    commitHandler.stop();
}

From source file:org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil.java

/**
 * Obtain an authentication token, for the specified cluster, on behalf of the current user
 * and add it to the credentials for the given map reduce job.
 *
 * The quorumAddress is the key to the ZK ensemble, which contains:
 * hbase.zookeeper.quorum, hbase.zookeeper.client.port and zookeeper.znode.parent
 *
 * @param job The job that requires the permission.
 * @param quorumAddress string that contains the 3 required configuratins
 * @throws IOException When the authentication token cannot be obtained.
 *//*from   ww  w . ja  va2s.c om*/
public static void initCredentialsForCluster(Job job, String quorumAddress) throws IOException {
    UserProvider userProvider = UserProvider.instantiate(job.getConfiguration());
    if (userProvider.isHBaseSecurityEnabled()) {
        try {
            Configuration peerConf = HBaseConfiguration.create(job.getConfiguration());
            ZKUtil.applyClusterKeyToConf(peerConf, quorumAddress);
            obtainAuthTokenForJob(job, peerConf, userProvider.getCurrent());
        } catch (InterruptedException e) {
            LOG.info("Interrupted obtaining user authentication token");
            Thread.interrupted();
        }
    }
}

From source file:LinkedTransferQueue.java

public boolean tryTransfer(E e, long timeout, TimeUnit unit) throws InterruptedException {
    if (e == null) {
        throw new NullPointerException();
    }//from  ww  w .j  a  va 2  s  .  co m
    if (xfer(e, TIMEOUT, unit.toNanos(timeout)) != null) {
        return true;
    }
    if (!Thread.interrupted()) {
        return false;
    }
    throw new InterruptedException();
}

From source file:org.apache.hadoop.hdfs.server.datanode.DataBlockScanner.java

public void run() {
    try {//from  www  .  j  av a  2s  .c om

        init();

        //Read last verification times
        if (!assignInitialVerificationTimes()) {
            return;
        }

        adjustThrottler();

        while (datanode.shouldRun && !Thread.interrupted()) {
            long now = System.currentTimeMillis();
            synchronized (this) {
                if (now >= (currentPeriodStart + scanPeriod)) {
                    startNewPeriod();
                }
            }
            if ((now - getEarliestScanTime()) >= scanPeriod) {
                verifyFirstBlock();
            } else {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ignored) {
                }
            }
        }
    } catch (RuntimeException e) {
        LOG.warn("RuntimeException during DataBlockScanner.run() : " + StringUtils.stringifyException(e));
        throw e;
    } finally {
        shutdown();
        LOG.info("Exiting DataBlockScanner thread.");
    }
}

From source file:uk.ac.horizon.ug.exploding.client.Client.java

/** internal async send */
@SuppressWarnings("unchecked")
private synchronized List<Message> sendQueuedMessageInternal(QueuedMessage qm) {
    log("sendQueuedMessage", "message", qm.message.toString());
    currentMessage = qm;//  w w  w  .j  a  v  a 2s. c  om
    sendMessageThread = Thread.currentThread();
    HttpPost request = new HttpPost(conversationUrl);
    Log.i(TAG, "SendMessages to " + request.getURI() + ", requestline=" + request.getRequestLine());
    request.setHeader("Content-Type", "application/xml;charset=UTF-8");
    //HttpURLConnection conn = (HttpURLConnection) conversationUrl.openConnection();
    XStream xs = new XStream(/*new DomDriver()*/);
    xs.alias("list", LinkedList.class);
    xs.alias("message", Message.class);

    // game specific
    ModelUtils.addAliases(xs);

    List<Message> messages = new LinkedList<Message>();
    messages.add(qm.message);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    xs.marshal(messages, new CompactWriter(new OutputStreamWriter(baos, Charset.forName("UTF-8"))));
    byte[] bytes = baos.toByteArray();
    Log.d(TAG, "Sent: " + bytes.length + " bytes");
    log("sendQueuedMessage.bytes", "bytes", bytes.length);
    request.setEntity(new ByteArrayEntity(bytes));

    synchronized (queuedMessages) {
        if (qm.cancelled) {
            currentMessage = null;
            return null;
        }
        qm.status = QueuedMessageStatus.SENDING;
    }

    HttpResponse reply = null;
    try {
        reply = httpClient.execute(request);
    } catch (Exception e) {
        Log.e(TAG, "Doing http request", e);
        log("sendQueuedMessage.error", "exception", e.toString());
        synchronized (queuedMessages) {
            currentMessage = null;
            if (qm.cancelled)
                return null;
            synchronized (qm) {
                qm.status = QueuedMessageStatus.DONE;
                qm.messageStatus = MessageStatusType.NETWORK_ERROR;
                qm.errorMessage = "Error sending request: " + e.getMessage() + ", URL=" + request.getURI();
            }
            return null;
        }
    }

    synchronized (queuedMessages) {
        if (qm.cancelled) {
            currentMessage = null;
            return null;
        }
        qm.status = QueuedMessageStatus.AWAITING_STATUS;
    }

    StatusLine statusLine = reply.getStatusLine();
    Log.d(TAG, "Http status: " + statusLine);
    int status = statusLine.getStatusCode();
    if (status != 200) {
        log("sendQueuedMessage.error", "status", status);
        if (reply.getEntity() != null)
            try {
                reply.getEntity().consumeContent();
            } catch (IOException e) {
                Log.d(TAG, "Ignored exception on " + status + " response: " + e);
            }

        synchronized (queuedMessages) {
            currentMessage = null;
            if (qm.cancelled)
                return null;
            synchronized (qm) {
                qm.status = QueuedMessageStatus.DONE;
                if (status == HttpStatus.SC_NOT_FOUND)
                    qm.messageStatus = MessageStatusType.NOT_FOUND;
                else if (status == HttpStatus.SC_INTERNAL_SERVER_ERROR)
                    qm.messageStatus = MessageStatusType.INTERNAL_ERROR;
                else if (status == HttpStatus.SC_BAD_REQUEST)
                    qm.messageStatus = MessageStatusType.INVALID_REQUEST;
                else if (status == HttpStatus.SC_FORBIDDEN)
                    qm.messageStatus = MessageStatusType.NOT_PERMITTED;
                else
                    qm.messageStatus = MessageStatusType.INTERNAL_ERROR;
                qm.errorMessage = "Error response (" + status + ") from server: " + statusLine.getReasonPhrase()
                        + ", URL=" + request.getURI();
            }
            return null;
        }
    }
    try {
        BufferedInputStream in = new BufferedInputStream(reply.getEntity().getContent());
        Log.d(TAG, "Reading HTTP response -> XML");
        messages = (List<Message>) xs.fromXML(new InputStreamReader(in, Charset.forName("UTF-8")));
        reply.getEntity().consumeContent();
    } catch (Exception e) {
        Log.e(TAG, "Reading response", e);
        log("sendQueuedMessage.error", "error", e.toString());
        synchronized (queuedMessages) {
            currentMessage = null;
            if (qm.cancelled)
                return null;
            synchronized (qm) {
                qm.status = QueuedMessageStatus.DONE;
                qm.messageStatus = MessageStatusType.NETWORK_ERROR;
                qm.errorMessage = "Error reading response: " + e.getMessage();
            }
            return null;
        }
    }
    log("sendQueueMessage.ok", "messages", Arrays.toString(messages.toArray()));
    Log.i(TAG, "Response " + messages.size() + " messages: " + messages);

    synchronized (queuedMessages) {
        currentMessage = null;
        if (qm.cancelled)
            return null;
        synchronized (qm) {
            qm.status = QueuedMessageStatus.DONE;
            qm.errorMessage = null;
            qm.messageStatus = MessageStatusType.OK;
            // check status(es)
            for (Message response : messages) {
                if (response.getType().equals(MessageType.ERROR.name())) {
                    qm.messageStatus = MessageStatusType.valueOf(response.getStatus());
                    qm.errorMessage = response.getErrorMsg();
                    break;
                }
            }
            if (!messages.isEmpty())
                qm.response = messages.get(0);
        }
    }
    if (Thread.interrupted())
        Log.d(TAG, "Client message Thread was intterupted");

    return messages;
}

From source file:LinkedTransferQueue.java

public E take() throws InterruptedException {
    Object e = xfer(null, WAIT, 0);
    if (e != null) {
        return cast(e);
    }/*  w  ww .  ja v a2s  .com*/
    Thread.interrupted();
    throw new InterruptedException();
}