CountDownLatch are used to control to flow of multiple running task. CDL delays the run time of tasks before the main thread is ran. Its simply a timer that counts down to zero and when timer hits zero the main thread is ran but during its count down it is also running other tasks .
import java.util.concurrent.CountDownLatch;
public class countdown
{
public static void main(String args[])
throws InterruptedException
{
// Creating CountDownLatch , there are 3 task being executed
CountDownLatch cdl = new CountDownLatch(3);
// Naming tasks and setting delay times
// 1 second delay
Task Task_One = new Task(1000, cdl, "task 1");
// 5 second delay
Task Task_Two = new Task(5000, cdl, "task 2");
// 3 second delay
Task Task_Three = new Task(3000, cdl, "task 3");
// Starting tasks
Task_One.start();
Task_Two.start();
Task_Three.start();
// The main task waits here
cdl.await();
// Main thread running
System.out.println(Thread.currentThread().getName() +
" You have hacked the main frame!");
}
}
// What main thread does while waiting
class Task extends Thread
{
private int dt;
private CountDownLatch cdl;
public Task(int dt, CountDownLatch cdl,
String name)
{
super(name);
this.dt = dt;
this.cdl = cdl;
}
@Override
public void run()
{
try
{
// task ran complete print finished
Thread.sleep(dt);
cdl.countDown();
System.out.println(Thread.currentThread().getName()
+ " finished");
}
// if not throw exception
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
Output :
task 1 finished
task 3 finished
task 2 finished
main You have hacked the main frame!
Android Studio Loading Animation Between Activities
Progress Dialog is dialog showing a progress indicator and an optional text message or view. The methods of Progress Dialog being used in this tutorial are: ProgressDialog.setTitle() – Used to set title of dialog box ProgressDialog.setMessage() – Used to set dialog message being displayed ProgressDialog.setProgressStyle() – Choose the style of indicator ProgressDialog.dismiss() – Dismiss the…
Android Studio Tutorial SeekBar
SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch thumb and drag left or right to set the current progress level or various other task. In this example, the seekbar will be used to display a percentage. As the user moves the SeekBar left to right the percentage value…
Python Ethereum Block Chain Interaction with Web3
This tutorial will show how to interact with the Ethereum blockchain using Python. To start, the Python library Web3 will need to be installed. The Web3 library allows you to interact with a local or remote Ethereum node, using a HTTP or IPC connection. Using Web3 you will be able to create smart contracts on…