Share

Search Java Code Snippets


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





#Java - Code Snippets for '#TimeUnit.SECONDS' - 7 code snippet(s) found

 Sample 1. Time Taken to call a method or service

Date date1 = new Date();

// Call Service or Method

Date date2 = new Date();
System.out.println(TimeUnit.SECONDS.convert(date2.getTime()-date1.getTime(), TimeUnit.MILLISECONDS));// Time Taken in Seconds
System.out.println(TimeUnit.MINUTES.convert(date2.getTime()-date1.getTime(), TimeUnit.MILLISECONDS));// Time Taken in Minutes
System.out.println(TimeUnit.HOURS.convert(date2.getTime()-date1.getTime(), TimeUnit.MILLISECONDS));// Time Taken in Hours

   Like      Feedback     Difference between 2 dates  Time to call a method  TimeUnit  Date.java.util.Date


 Sample 2. 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


 Sample 3. Code Sample / Example / Snippet of org.apache.storm.trident.windowing.WindowsStoreFactory

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

Config conf = new Config();

WindowsStoreFactory mapState = new InMemoryWindowsStoreFactory();



if (args.length == 0) {

List<? extends WindowConfig> list = Arrays.asList(

SlidingCountWindow.of(1000, 100)

,TumblingCountWindow.of(1000)

,SlidingDurationWindow.of(new BaseWindowedBolt.Duration(6, TimeUnit.SECONDS), new BaseWindowedBolt.Duration(3, TimeUnit.SECONDS))

,TumblingDurationWindow.of(new BaseWindowedBolt.Duration(3, TimeUnit.SECONDS))

);



for (WindowConfig windowConfig : list) {

LocalCluster cluster = new LocalCluster();

cluster.submitTopology("wordCounter", conf, buildTopology(mapState, windowConfig));

Utils.sleep(60 * 1000);

cluster.shutdown();

}

System.exit(0);

} else {

conf.setNumWorkers(3);

StormSubmitter.submitTopologyWithProgressBar(args[0], conf, buildTopology(mapState, SlidingCountWindow.of(1000, 100)));

}

}


   Like      Feedback      org.apache.storm.trident.windowing.WindowsStoreFactory


 Sample 4. Code Sample / Example / Snippet of org.apache.storm.topology.base.BaseWindowedBolt

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

TopologyBuilder builder = new TopologyBuilder();

BaseWindowedBolt bolt = new SlidingWindowSumBolt()

.withWindow(new Duration(5, TimeUnit.SECONDS), new Duration(3, TimeUnit.SECONDS))

.withTimestampField("ts")

.withLag(new Duration(5, TimeUnit.SECONDS));

builder.setSpout("integer", new RandomIntegerSpout(), 1);

builder.setBolt("slidingsum", bolt, 1).shuffleGrouping("integer");

builder.setBolt("printer", new PrinterBolt(), 1).shuffleGrouping("slidingsum");

Config conf = new Config();

conf.setDebug(true);



if (args != null && args.length > 0) {

conf.setNumWorkers(1);

StormSubmitter.submitTopologyWithProgressBar(args[0], conf, builder.createTopology());

} else {

LocalCluster cluster = new LocalCluster();

cluster.submitTopology("test", conf, builder.createTopology());

Utils.sleep(40000);

cluster.killTopology("test");

cluster.shutdown();

}

}


   Like      Feedback      org.apache.storm.topology.base.BaseWindowedBolt


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 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 6. Code Sample / Example / Snippet of java.util.concurrent.Semaphore

    private void removeRepository(String instanceName) throws IOException, InterruptedException, InvalidSyntaxException {

Configuration[] configs = listConfigurations("(factory.instance.pid=" + instanceName + ")");

if ((configs != null) && (configs.length > 0)) {

final Semaphore sem = new Semaphore(0);

ServiceTracker<Object, Object> tracker = new ServiceTracker<Object, Object>(m_bundleContext, m_bundleContext.createFilter("(factory.instance.pid=" + instanceName + ")"), null) {

@Override

public void removedService(ServiceReference<Object> reference, Object service) {

super.removedService(reference, service);

sem.release();

}

};

tracker.open();



try {

configs[0].delete();



if (!sem.tryAcquire(1, TimeUnit.SECONDS)) {

throw new IOException("Instance did not get removed in time.");

}

}

finally {

tracker.close();

}

}

}


   Like      Feedback      java.util.concurrent.Semaphore


 Sample 7. Code Sample / Example / Snippet of java.util.concurrent.ExecutorService

    public void hundredStreamsConcurrently() throws Exception {

ExecutorService e = Executors.newFixedThreadPool(5);

for (int i = 0; i < 10; i++) {

e.execute(new Runnable() {

public void run() {

for (int i = 0; i < 10; i++) {

try {

isJarInputStreamReadable();

}

catch (Exception e) {

m_failure = e;

}

}

}

});

}

e.shutdown();

e.awaitTermination(10, TimeUnit.SECONDS);



assert m_failure == null : "Test failed: " + m_failure.getLocalizedMessage();

}




   Like      Feedback      java.util.concurrent.ExecutorService



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



comments powered by Disqus