Minesweeper is a classic video game created in 1990’s., The goal of this game is to find all the squares which dont have mines underneath. The game is designed with the logic of each squares or cells have following rules. Each square may be any one of the following: Contains a Mine which will explode … Continue reading “Minesweeper In Java”
Author: admin
Sudoku Solver Java
Sudoku is an interesting game of filling numbers from 1 to 9 with the logic of non repeating filling of numbers., which improves our solving skills and increase our solving strategies. The following code is developed in Java to solve any Sudoku game., by following Sudoku rules and also by randomly trying numbers if there … Continue reading “Sudoku Solver Java”
forEach Expression
Starting Java 8, a new method forEach is introduced to iterate the elements. This forEach(Consumer<? super T> action) method is created in the following interfaces in java. java.lang.Iterable java.util.Stream This method performs a user given action on each element. In the below example, we can iterate a list of collections with forEach defining a condition.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.ArrayList; import java.util.List; public class ForEachExample { public static void main(String args[]) { List numberList = new ArrayList(); numberList.add(1); numberList.add(2); numberList.add(3); numberList.add(4); numberList.add(5); numberList.add(6); numberList.add(7); numberList.add(8); System.out.println("All Numbers"); numberList.forEach((numValue)->{ System.out.println(""+numValue); }); System.out.println("Even Numbers"); numberList.forEach((numValue)->{ if(numValue%2==0) { System.out.println(""+numValue); } }); System.out.println("Stream Filter - Odd Numbers"); numberList.stream() .filter(numValue->(numValue%2==1)) .forEach(System.out::println); } } |
Result:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
All Numbers 1 2 3 4 5 6 7 8 Even Numbers 2 4 6 8 Stream Filter - Odd Numbers 1 3 5 7 |
Lambda Expressions
Lambda expression is a Java 8 feature, which helps the developer to simplify syntax of anonymous interface implementation whenever required. Interface is defined anonymously in earlier Java Implementation in the below old syntax., which is very much reduced in Java 8 lambda implementation, comparison of the below example provide a clear view of Lambda expression. … Continue reading “Lambda Expressions”
ForkJoinPool
ForkJoinPool is the core of the concurrency frameworks which follows divide and conquer approach in completing the tasks. Fork – Recursively break the single task into multiple smaller tasks till the threshold. Join – Join the multiple smaller tasks results and produce the output recursively. Initialise the maximum number of worker threads.
|
1 |
ForkJoinPool forkJoinPool = new ForkJoinPool(3); |
RecursiveAction class … Continue reading “ForkJoinPool”
Reentrant Lock
The ReentrantLock class is an implementation of Lock Interface, which will lock() the execution block for the thread which calls it.Until the locked thread calls the lock.unlock() method, the code block in between will not be available to other Threads…even when it is idle or sleeping or taking long time.Hence the block after lock.lock() is … Continue reading “Reentrant Lock”
Cyclic Barrier
Assume a the conference call will be opened when 4 users join., Assume until all the users join, that conference call wont be opened even when the less than 4 users join. Below example is a simple illustration of the scenario using Cyclic Barrier.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
package my.test.learning; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; public class CyclicBarrierExample { public static void main(String[] args) { CyclicBarrier barrier = new CyclicBarrier(4); ChatUser user1 = new ChatUser("Jake", barrier); ChatUser user2 = new ChatUser("Mike", barrier); ChatUser user3 = new ChatUser("Don", barrier); ChatUser user4 = new ChatUser("Kar", barrier); user1.start(); user2.start(); user3.start(); user4.start(); } } class ChatUser extends Thread { private String username; boolean join = false; boolean conferenceCallOpened = false; private CyclicBarrier cyclicBarrier; public ChatUser(String username, CyclicBarrier cyclicBarrier) { this.username = username; this.cyclicBarrier = cyclicBarrier; } @Override public void run() { joinedConference(); try { cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } openConferenceCall(); } public void joinedConference() { join = true; System.out.println(username + " has joined the conference call.."); } public void openConferenceCall() { conferenceCallOpened = true; System.out.println("Conference call Opened"); // May be can write any function to open the conference call... } } |
Result:
|
1 2 3 4 5 6 7 8 |
Mike has joined the conference call.. Kar has joined the conference call.. Jake has joined the conference call.. Don has joined the conference call.. Conference call Opened Conference call Opened Conference call Opened Conference call Opened |
Executor & Executor Service
Executor and ExecutorService are the utility classes present starting from JDK 1.5 for executing a java process/block parallely in a controlled manner. Executor – Executor is used for creates a thread pool to for fixed number, using the method newFixedThreadPool. e.g., We have a 2 dimensional array where each row needs to be processed or set to … Continue reading “Executor & Executor Service”
Semaphore
A semaphore maintains a fixed number of threads in executions if exceeds lock can be acquired for that semaphore thread. Below method will get the lock of semaphore within semaphore.availablePermits()
|
1 |
semaphore.acquire() |
If there are more that fixed threads, it will wait to acquire/get the semaphore lock until it has semaphore.availablepermits() greater than ‘0’.A semaphore initialised to … Continue reading “Semaphore”
Binary To Decimal Conversions
This example shows the conversion of Decimal to Binary & Binary to Decimal in Java. The toBinary() method in BinaryConversion class uses the recursion logic of modulo(%) 2 of given number(which is reminder), and calls the same method recursively with number/2( from 512>>2 =256) , until the reminder is one. The toDecimal() method uses the … Continue reading “Binary To Decimal Conversions”