Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Thread.sleep' - 11 code snippet(s) found

 Sample 1. Clear Map entries after expiration time using Apache commons PassiveExpiringMap

PassiveExpiringMap<String,String> cache = new PassiveExpiringMap<String,String>(1000); // Expiration time of 1 sec
cache.put("Key1", "Value1");
System.out.println(cache.containsKey("Key1"));
Thread.sleep(500);
System.out.println(cache.containsKey("Key1"));
Thread.sleep(500);
System.out.println(cache.containsKey("Key1"));

   Like      Feedback     expiring cache using map   Clear Map entries after expiration time   PassiveExpiringMap


 Sample 2. Write a program to show thread usage in Java by implementing runnable interface

public class MyClass {
   static class MyThreadClass implements Runnable{

      public void start() {
         Thread t = new Thread(this,"threadName");
         t.start();
      }
      
      @Override
      public void run() {
         System.out.println("Hello");
         try {
            Thread.sleep(1000);
            System.out.println("Hello Again");
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
         
      }
      
   }
   
   public static void main(String[] args){
      MyThreadClass myThreadClass = new MyThreadClass();
      myThreadClass.start();
      
      MyThreadClass myThreadClass2 = new MyThreadClass();
      myThreadClass2.start();
   }
}

   Like      Feedback     Threads  runnable interface


 Sample 3. Retry in case of exception

public static final int NUMBER_OF_RETRIES = 5;

try {
   // do something
} catch (Exception e) {
   int count;
   for (count = 1; count <= NUMBER_OF_RETRIES; count++) {
      Thread.sleep(5000);
   } catch (InterruptedException e1) {
      try {
         // do something again
         break;
      } catch (Exception ex) {
   }
}

   Like      Feedback     exception handling   exception   Retry in case of exception   for loop   for   Thread.sleep   InterruptedException


 Sample 4. Clear Map entries after expiration time using Apache commons PassiveExpiringMap

PassiveExpiringMap<String,String> cache = new PassiveExpiringMap<String,String>(1,TimeUnit.SECONDS); // Expiration time of 1 sec
cache.put("Key1", "Value1");
System.out.println(cache.containsKey("Key1")); // prints true
Thread.sleep(1000);
System.out.println(cache.containsKey("Key1")); // prints false

   Like      Feedback     Clear Map entries after expiration  PassiveExpiringMap  map  apache commons  TimeUnit.SECONDS  Thread.sleep


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 5. Code Sample / Example / Snippet of org.apache.spark.SparkConf

  public static void main(String[] args) throws Exception {

SparkConf sparkConf = new SparkConf().setAppName(APP_NAME);

final JavaSparkContext sc = new JavaSparkContext(sparkConf);



JavaRDD<Integer> rdd = sc.parallelize(Arrays.asList(1, 2, 3, 4, 5), 5).map(

new IdentityWithDelay<Integer>());

JavaFutureAction<List<Integer>> jobFuture = rdd.collectAsync();

while (!jobFuture.isDone()) {

Thread.sleep(1000); // 1 second

List<Integer> jobIds = jobFuture.jobIds();

if (jobIds.isEmpty()) {

continue;

}

int currentJobId = jobIds.get(jobIds.size() - 1);

SparkJobInfo jobInfo = sc.statusTracker().getJobInfo(currentJobId);

SparkStageInfo stageInfo = sc.statusTracker().getStageInfo(jobInfo.stageIds()[0]);

System.out.println(stageInfo.numTasks() + " tasks total: " + stageInfo.numActiveTasks() +

" active, " + stageInfo.numCompletedTasks() + " complete");

}



System.out.println("Job results are: " + jobFuture.get());

sc.stop();

}


   Like      Feedback      org.apache.spark.SparkConf


 Sample 6. Code Sample / Example / Snippet of org.apache.spark.SparkJobInfo

  public static void main(String[] args) throws Exception {

SparkConf sparkConf = new SparkConf().setAppName(APP_NAME);

final JavaSparkContext sc = new JavaSparkContext(sparkConf);



JavaRDD<Integer> rdd = sc.parallelize(Arrays.asList(1, 2, 3, 4, 5), 5).map(

new IdentityWithDelay<Integer>());

JavaFutureAction<List<Integer>> jobFuture = rdd.collectAsync();

while (!jobFuture.isDone()) {

Thread.sleep(1000); // 1 second

List<Integer> jobIds = jobFuture.jobIds();

if (jobIds.isEmpty()) {

continue;

}

int currentJobId = jobIds.get(jobIds.size() - 1);

SparkJobInfo jobInfo = sc.statusTracker().getJobInfo(currentJobId);

SparkStageInfo stageInfo = sc.statusTracker().getStageInfo(jobInfo.stageIds()[0]);

System.out.println(stageInfo.numTasks() + " tasks total: " + stageInfo.numActiveTasks() +

" active, " + stageInfo.numCompletedTasks() + " complete");

}



System.out.println("Job results are: " + jobFuture.get());

sc.stop();

}


   Like      Feedback      org.apache.spark.SparkJobInfo


 Sample 7. Code Sample / Example / Snippet of org.apache.spark.SparkStageInfo

  public static void main(String[] args) throws Exception {

SparkConf sparkConf = new SparkConf().setAppName(APP_NAME);

final JavaSparkContext sc = new JavaSparkContext(sparkConf);



JavaRDD<Integer> rdd = sc.parallelize(Arrays.asList(1, 2, 3, 4, 5), 5).map(

new IdentityWithDelay<Integer>());

JavaFutureAction<List<Integer>> jobFuture = rdd.collectAsync();

while (!jobFuture.isDone()) {

Thread.sleep(1000); // 1 second

List<Integer> jobIds = jobFuture.jobIds();

if (jobIds.isEmpty()) {

continue;

}

int currentJobId = jobIds.get(jobIds.size() - 1);

SparkJobInfo jobInfo = sc.statusTracker().getJobInfo(currentJobId);

SparkStageInfo stageInfo = sc.statusTracker().getStageInfo(jobInfo.stageIds()[0]);

System.out.println(stageInfo.numTasks() + " tasks total: " + stageInfo.numActiveTasks() +

" active, " + stageInfo.numCompletedTasks() + " complete");

}



System.out.println("Job results are: " + jobFuture.get());

sc.stop();

}


   Like      Feedback      org.apache.spark.SparkStageInfo


 Sample 8. Code Sample / Example / Snippet of org.apache.storm.testing.MemoryTransactionalSpout

  public static void main(String[] args) throws Exception {

MemoryTransactionalSpout spout = new MemoryTransactionalSpout(DATA, new Fields("word"), PARTITION_TAKE_PER_BATCH);

TransactionalTopologyBuilder builder = new TransactionalTopologyBuilder("global-count", "spout", spout, 3);

builder.setBolt("partial-count", new BatchCount(), 5).noneGrouping("spout");

builder.setBolt("sum", new UpdateGlobalCount()).globalGrouping("partial-count");



LocalCluster cluster = new LocalCluster();



Config config = new Config();

config.setDebug(true);

config.setMaxSpoutPending(3);



cluster.submitTopology("global-count-topology", config, builder.buildTopology());



Thread.sleep(3000);

cluster.shutdown();

}


   Like      Feedback      org.apache.storm.testing.MemoryTransactionalSpout


 Sample 9. Code Sample / Example / Snippet of org.apache.storm.transactional.TransactionalTopologyBuilder

  public static void main(String[] args) throws Exception {

MemoryTransactionalSpout spout = new MemoryTransactionalSpout(DATA, new Fields("word"), PARTITION_TAKE_PER_BATCH);

TransactionalTopologyBuilder builder = new TransactionalTopologyBuilder("global-count", "spout", spout, 3);

builder.setBolt("partial-count", new BatchCount(), 5).noneGrouping("spout");

builder.setBolt("sum", new UpdateGlobalCount()).globalGrouping("partial-count");



LocalCluster cluster = new LocalCluster();



Config config = new Config();

config.setDebug(true);

config.setMaxSpoutPending(3);



cluster.submitTopology("global-count-topology", config, builder.buildTopology());



Thread.sleep(3000);

cluster.shutdown();

}


   Like      Feedback      org.apache.storm.transactional.TransactionalTopologyBuilder


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 10. Code Sample / Example / Snippet of java.util.concurrent.CountDownLatch

    public void testTooLongTask() throws Exception {

final CountDownLatch latch = new CountDownLatch(5);



Executer executer = new Executer(new Runnable() {

public void run() {

try {

Thread.sleep(20);

latch.countDown();

}

catch (InterruptedException e) {

e.printStackTrace();

}

}

});

executer.start(10);

assert latch.await(1, TimeUnit.SECONDS);

}


   Like      Feedback      java.util.concurrent.CountDownLatch


 Sample 11. Write a Program to show Java thread usage by extending Thread class

public class MyClass {
   static class MyThreadClass extends Thread{

      @Override
      public void run() {
         System.out.println("Hello");
         try {
            Thread.sleep(1000);
            System.out.println("Hello Again");
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
         
      }
      
   }
   
   public static void main(String[] args){
      MyThreadClass myThreadClass = new MyThreadClass();
      myThreadClass.start();
      
      MyThreadClass myThreadClass2 = new MyThreadClass();
      myThreadClass2.start();
   }
}

   Like      Feedback     Threads  Thread class



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner