Maven builds a project using its project object model (POM) and a set of plugins. Dependencies used by the project are mentioned in the pom.xml file. We can also get the dependencies artifact from maven along with source codes.
How to download source for maven artifacts
Maven builds a project using its project object model (POM) and a set of plugins. Dependencies used by the project are mentioned in the pom.xml file. We can also get the dependencies artifact from maven along with source codes.
In the following section, we will cover two use cases
1) How to generate source code for third-party artifacts that my project is using
2) How to generate source code for my project
Download sources for third party artifacts
Executing the following command from the terminal will download the sources for all the artifacts for the project and will attach it to Eclipse IDE.
mvn eclipse:eclipse -DdownloadSources=true
Executing the following command from the terminal will download the sources for all the artifacts only, attaching the sources in the IDE with the binaries needed to be done seperately..
mvn dependency:sources -Dsilent=true
Generate source code for project
This command will generate the sources jar under the target folder for the project.
This article summarizes different ways to use the Random() class in java to generate bounded or unbounded random numbers. It also shows another important feature of the class that is using seed to generate same set of random numbers.
Generating Random Number in Java
This article summarizes different ways to use the Random() class in java to generate bounded or unbounded random numbers. It also shows another important feature of the class that is using seed to generate same set of random numbers.
java.util.Random used used to generate bounded or unbounded random numbers. It supports specifying a seed which is used to set the internal state of the pseudorandom number generator.
Random(): creates new random generator
Random(long seed): creates new random generator using specified seed
Unbounded generate 5 random numbers
Following code prints 5 random numbers unbounded without any seed
public static void generateRadom() {
System.out.println("\nPrinting 10 Random Numbers");
Random generator = new Random();
for (int i = 0; i < 5; i++) {
System.out.print(generator.nextInt() + " ");
}
}
Invocation 1
Printing 5 Random Numbers
-1937915077 75383412 -901884443 1725835531 1371480362
Invocation 2
Printing 5 Random Numbers
-1261854044 328673857 -1787159304 446964878 -283294822
Notice that in both the invocations the generated numbers are different. This is because we have not set any seed in the Random Class.
Random Bounded Number
Following method will generate 5 random numbers between 0 and 99
public static void generateRadomBounded() {
System.out.println("\nPrinting 5 Random Numbers Between 0 and 99");
Random generator = new Random();
for (int i = 0; i < 5; i++) {
System.out.print(generator.nextInt(100) + " ");
}
}
Invocation 1
Printing 5 Random Numbers Between 0 and 99
25 95 13 60 67
Invocation 2
Printing 5 Random Numbers Between 0 and 99
17 10 21 96 15
Generate Random number bounded with a seed
Bellow function will generate bounded 5 random numbers but since we are setting a seed, if the seed is same on multiple invocation then each invocation will generate the same set of random numbers.
public static void generateRadomWithSeed(long seed) {
System.out.println("\nPrinting 5 Random Numbers Between 0 and 99 with seed");
Random generator = new Random(seed);
for (int i = 0; i < 5; i++) {
System.out.print(generator.nextInt(100) + " ");
}
}
Invocation 1
Printing 5 Random Numbers Between 0 and 99 with seed 4
62 52 3 58 67
Invocation 2
Printing 5 Random Numbers Between 0 and 99 with seed 4
62 52 3 58 67
Summary
If we want to generate the same set of random numbers, we can set seed.
Test cases can use seed to make sure it gets same set of random numbers.
Sometimes we want to remove the reference to existing repository details, such as cloned from github so that we can check the code in a different git repository or a different version control system. This article covers the steps that we can take to remove git references.
If we clone a repo or fork a repo from git, the newly created repository will have git references as version control. If we want to remove the git references from the new repository then we need to delete all the git references. Once we remove the git references then it will not have any reference to old repository and we can use this project folder to create a brand new repository in git or any other version control system. This article talks about how to remove
Sometimes we want to remove the reference to existing repository details, such as cloned from github so that we can check the code in a different git repository or a different version control system. This article covers the steps that we can take to remove git references.
If we clone a repo or fork a repo from git, the newly created repository will have git references as version control. If we want to remove the git references from the new repository then we need to delete all the git references. Once we remove the git references then it will not have any reference to old repository and we can use this project folder to create a brand new repository in git or any other version control system. This article talks about how to remove
The good thing is that all the git related information is stored in a folder .git . Normally we would have only one .git folder in the root of the repository, but we might have more than one .git folder based on how the repository is configured. Apart from .git folder we might have following git specific files,
.gitignore: allows to include/exclude files to be synced to git remote server
.gitkeep: allows us to include an empty directory to be synced to git remote server
. gitattributes: allows us to ensure consistent git settings across the machine.
Essentially we need to delete all these git related folder/files that will make our repository clean and without any git version details. The following sections show how to achieve the same in a Windows environment and Mac environment
Windows
The rmdir or rd command will not delete/remove any hidden files or folders within the directory you specify, so we should use the del command to be sure that all files are removed from the .git folder.
Open the command prompt
Navigate to the project directory, i.e. - cd path_to_your_repository
del /F /S /Q /A .git
rmdir .git
Mac
Open a terminal and navigate to the directory of your project, i.e. - cd path_to_your_repository. And run the following commands
Go to the directory of your project and run the following command which finds the only the folder with the name .git and prints its path in the console.
This blog post drives though an implementation pattern and a working setup with multiple microservices where they get registered with Eureka and the client can discover them and invoke them
Microservices Registration and Discovery using Spring Cloud, Eureka
This blog post drives though an implementation pattern and a working setup with multiple microservices where they get registered with Eureka and the client can discover them and invoke them
What is Eureka
Eureka is a REST-based service that provides support for discovering other services so that clients can use the Eureka service to discover services and calling those services.
Why we need Discovery Service
In a Microservices architecture where we have multiple services running, we need a mechanism to know what are the services and how to invoke them. Each service typically will have a different URL or IP address to reach out to. Also, clients of those microservices need to know when a new
microservice is added or an existing microservice was taken down for maintenance. Discovery service provides that mechanism, where services can register themself in the discovery service and clients, can query the discovery service to know more about the available microservices.
Project achievement
In this project, we will go through an end to end use case where we will have a client microservice calling another microservice utilizing Eureka discovery service. Essentially we want to find existing services by looking up in discovery service and then invoking the target service without knowing about their whereabouts like URL, or IP or port, etc. For that, we will build a discovery server one service microservice and one client microservice.
Project Contents
Eureka Discovery Service: Provides the registration and discovery
Article Microservice: Provides REST services for Article CRUD operation
Client Microservice: Invokes the article microservice
Above pictures show what we are going to achieve in very high level
1) We will deploy a Discovery service
2) We will deploy Article Service which registers itself with discovery service with a unique name.
3) We will deploy a consumer application which is another spring boot application which will invoke the
Article Service by its name.
Technology Used
Java 11
Apache Maven 3.5.0
Spring Boot 2.2.6
Spring Cloud Hoxton.SR1
Netflix Ribbon 2.3.0
Open Feign
JSON
Discovery Server
The following section highlights the important code related to Eureka Server. The details can be looked into in the git repo. Mainly we need to add the maven dependency for Eureka Discovery Server, in the spring boot properties file add the server port and defaultZone and annotate the SpringBoot Application class with @EnableEurekaServer.
We need to include the Netflix Eureka Server dependency in the maven pom file.
server.port=8761
##Eureka Related
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/
eureka.client.healthcheck.enabled=true
The Eureka Discovery Server is a Spring Boot application that is very simple. We need to annotate the application with @EnableEurekaServer.
@EnableEurekaServer allows this application to run as a Eureka server, aided by Spring Boot to instantiate Eureka-related beans based on configuration properties.
ServiceDiscovery.java
package com.bootng.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class ServiceDiscovery {
public static void main(String[] args) {
SpringApplication.run(ServiceDiscovery.class, args);
}
}
Now when we build and start the discovery server and access the server through http://localhost:8761 we would see something like bellow. Notice the following screenshots shows no entries under applications because no other services are up yet.
Discovery Service UI
Article Service
Now we will build and deploy the Article microservice which is another spring boot application that registers itself with the discovery service.
The name with which this service is registered is "article-service" and is defined in bootstrap.properties file.
## Server Related
server.port=9051
## Eureka Related
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=false
eureka.vipAddress=article-service
bootstrap.properties
spring.application.name=article-service
Following is the main application class representing Article Microservice.
ArticleMicroservice
@ComponentScan({ "com.siddb" })
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ArticleMicroservice {
public static void main(String args[]) {
SpringApplication.run(ArticleMicroservice.class, args);
}
}
This Spring Boot Application exposes the following REST endpoints through the ArticleController.java class.
And registers itself with the service discovery with the name "article-service"
ArticleController.java
@RestController
@RequestMapping("/api")
public class ArticleController {
private static final Logger log = LoggerFactory.getLogger(ArticleController.class);
@Autowired
ArticleService articleService;
@RequestMapping(value = {"/articles"}, method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Article>> getArticles() throws AppException {
List<Article> articles = articleService.getArticles();
return new ResponseEntity<List<Article>>(articles, HttpStatus.OK);
}
@RequestMapping(value = {"/articles/{id}"}, method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Article> getArticle(@PathVariable(value = "") String id)
throws AppException, NotFoundException {
Article articles = articleService.getByID(id);
return new ResponseEntity<Article>(articles, HttpStatus.OK);
}
//other methods for POST and Deleted removed from here for readability.
}
[
{
"id": "America-Travel",
"name": "America Travel",
"description": "Places to travel in AmericaLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum",
"category": "Travel"
}
]
GET http://localhost:9051/api/articles
Sample Response:
[
{
"id": "America-Travel",
"name": "America Travel",
"description": "Places to travel in AmericaLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum",
"category": "Travel"
}
]
[
{
"id": "America-Travel",
"name": "America Travel",
"description": "Places to travel in AmericaLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum",
"category": "Travel"
}
]
Article Client Service
Now we will build and deploy the Client Service application which is another Spring Boot application that consumes the REST services deployed by Article Service. Most interesting part
Following is the Consumer Application Class. We have annotated it with @EnableDiscoveryClient so that it can discover other services registered with the Discovery Server.
ConsumerApplication
@ComponentScan({"com.siddb", "com.siddb.controller"})
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
public static void main(String args[]) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
Following is the main controller which uses the discovery service internally and invokes services on the service article-service using ribbon. From the ribbon configuration in (application.properties), it knows article-service-consumer is tied to a service with VIP Address (name) article-service.
RibbonController.java
@RestController
public class RibbonController {
@Autowired
private RestTemplate restTemplate;
/**
* This method calls the microservice GET article-service/api/articles using Ribbon to get a list of
* Articles and returns the same. it uses RestTemplate to make the API calls.
* @return
* @throws Exception
*/
@RequestMapping(value = {"render"}, method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> renderArticles() throws Exception {
JsonNode repos =
restTemplate.getForObject("http://article-service-consumer/api/articles", JsonNode.class);
int counter = 1;
StringBuilder result = new StringBuilder("\n List of Articles");
if (repos.isArray()) {
for (JsonNode jsonNode : repos) {
result.append("\n Repo ").append(counter++).append("::");
result.append(jsonNode.get("name").asText());
}
}
return new ResponseEntity<String>(result.toString(), HttpStatus.OK);
}
}
The Consumer Controller uses the ribbon configuration that we have added in the property file to invoke the article service by the ribbon name "article-service-consumer". This is where the Consumer controller will discover the actual API for the corresponding service name.
And call GET http://article-service-consumer/api/articles will be actually to http://localhost:9051/api/articles. This is how using the Eureka Discovery service the client becomes independent of the service (IP address, port, etc.). The client simply can refer to the service with its published name.
Start Microservices
Now the fun part, start all the microservices and invoke the /render REST API. We need to start all services (discovery service, article service, consumer service).
For that, we first need to check out the repo from git and build/run each project.
1) git clone https://github.com/siddharthagit/spring-boot-sdc.
2) go to the root folder that contains this repo.
3) open 3 Terminals one for each subproject (eureka-service-discovery, ms-article-service, ms-consumer-service).
4) build each project and start the services.
mvn clean install
mvn spring-boot:run
5) Check the status of each service on the discovery server http://localhost:8761
6)Invoke the REST API on consumer service http://localhost:9053/render
This API internally will use the discovery server to resolve the service and will route the call to http://localhost:9051/api/articles.
In this article, we've covered how to use Spring Cloud Eureka for service discovery in the microservice/cloud environment. We created two simple REST services that communicate with each other without hardcoding any hostname/port while making REST calls. Though in this article we have shown how the client can invoke the article service with ribbon, the git repo also contains a Feign client that uses Feign and will be covered in a different article.
In This Article we will see how we can convert Integer to Binary number. We will see some built in options that Java provides as well as two different custom implementation.
In This Article we will see how we can convert Integer to Binary number. We will see some built in options that Java provides as well as two different custom implementation.
Using Integer.toBinaryString
Using Integer.toBinaryString() method we can convert a integer to Binary String very easily
We can also use Integer.toString() and pass the number and radix to convert the integer to String. In this case we pass 2 as radix to convert the Integer to Binary Number String.
Following code is a custom method to convert an Integer to Binary.
private static String toBinary_divide(Integer i) {
StringBuilder ret = new StringBuilder();
while (i > 0) {
if (i % 2 == 1) {
ret.insert(0, "1");
}
else {
ret.insert(0, "0");
}
i = i / 2;
}
return ret.toString();
}
By using Bit masking
This is another custom method to convert Integer to Binary String. This Algorithm works by masking each bit in the original number and creating a char array.
And finally converting the char array to String.
private static String toBinary_mask(Integer num) {
char ret[] = new char[32];
for (int i = 0; i < 32; i++) {
int mask = 1 << i;
ret[31 - i] = (num & mask) != 0 ? '1' : '0';
}
return new String(ret);
}
Full Code
IntegerToBinaryConverter.java
import java.math.BigInteger;
public class IntegerToBinaryConverter {
public static void main(String[] args) {
System.out.println("Convert to Binary 2020 = " + toBinary1(2020));
System.out.println("Convert to Binary 2020 = " + toBinary2(2020));
System.out.println("Convert to Binary 2020 = " + toBinary3(2020));
System.out.println("Convert to Binary 2020 = " + toBinary_divide(2020));
System.out.println("Convert to Binary 2020 = " + toBinary_mask(2020));
}
private static String toBinary1(Integer i) {
return Integer.toBinaryString(i);
}
private static String toBinary2(Integer i) {
return Integer.toString(i, 2);
}
private static String toBinary3(Integer i) {
BigInteger bigInt = new BigInteger(String.valueOf(i));
return bigInt.toString(2);
}
private static String toBinary_divide(Integer i) {
StringBuilder ret = new StringBuilder();
while (i > 0) {
if (i % 2 == 1) {
ret.insert(0, "1");
}
else {
ret.insert(0, "0");
}
i = i / 2;
}
return ret.toString();
}
private static String toBinary_mask(Integer num) {
char ret[] = new char[32];
for (int i = 0; i < 32; i++) {
int mask = 1 << i;
ret[31 - i] = (num & mask) != 0 ? '1' : '0';
}
return new String(ret);
}
}
Console Output
Convert to Binary 2020 = 11111100100
Convert to Binary 2020 = 11111100100
Convert to Binary 2020 = 11111100100
Convert to Binary 2020 = 11111100100
Convert to Binary 2020 = 00000000000000000000011111100100
Summary
This article shows different ways to convert the Integer to Binary String. The Bit masking is very efficient and in fact the builtin methods like Integer.toString() uses Bit masking internally.
For more information on Bit masking the links provided in Reference section is very useful.
This article covers the datatypes available for JavaScript. The latest ECMAScript standard defines nine types: 7 Primitive types and 2 Structural Types.
This article covers the datatypes available for JavaScript. The latest ECMAScript standard defines nine types: 7 Primitive types and 2 Structural Types.
Primitive Types
The six primitive types are Boolean, Null, Undefined, String, Number, BigInt, and Symbol.
Boolean type
Boolean represents a logical entity and can have two values: true and false.
The Boolean object will be with the initial true only if the parameter passed is an object, array, or true. Else the Boolean object will be false.
Following code shows the different values the Boolean object will have when passed different parameters.
var b1 = new Boolean();
console.log(b1); //false
var b2 = new Boolean(false);
console.log(b2);//false
var b3 = new Boolean(0);
console.log(b3);//false
var b4 = new Boolean(NaN);
console.log(b4);//false
var b5 = new Boolean(undefined);
console.log(b5);//false
var b6 = new Boolean(null);
console.log(b6); //false
var b7 = new Boolean({});
console.log(b7);//true
var b8 = new Boolean(true);
console.log(b8);//true
var b9 = new Boolean([]);
console.log(b9);//true
Null type
The Null type has exactly one value: null.
Every Object is derived from null value, and therefore typeof operator returns object for it.
The following code shows one important feature of null, it is of type object.
var n1 = null;
console.log(n1); //null
console.log(typeof n1); //object
Undefined type
A variable that has not been assigned a value has the value undefined.
The following code shows one example where a variable u1 is defined but not assigned any value.
var u1 ;
console.log(typeof u1);//undefined
console.log(u1);//undefined
String
The string type is used to represent textual data. JavaScript strings are immutable. This means that once a string is created, it is not possible to modify it
Strings can be created as primitives, from string literals, or as objects, using the String() constructor. In case of primitive typeof returns "string" otherwise it returns "object". String object can be converted to string primitives using valueOf() method on the String object.
The Number type is a double-precision 64-bit binary format IEEE 754 value (numbers between -(2^53 − 1) and 2^53 − 1). In addition to representing floating-point numbers, the number type has three symbolic values: +Infinity, -Infinity, and NaN ("Not a Number").
Number.MAX_VALUE or Number.MIN_VALUE represents the maximum and minimum values possible using numbers.
The BigInt type is a numeric primitive in JavaScript that can represent integers with arbitrary precision.
A BigInt is created by appending n to the end of an integer or by calling the constructor.
BigInts cannot be operated on interchangeably with Numbers. Instead, a TypeError will be thrown.
Following code shows how to find the value of 2^53, we have choosen 53 because then the product value will be beyond the capacity of numbers. If you
Roman numerals have seven symbols. The table below shows these symbols and their decimal equivalents.
Symbol
Number
I
1
V
5
X
10
L
50
C
100
D
500
M
1,000
Negative Numbers and Range
We cannot represent negative numbers using Roman Numbers.
The largest number you can write in Roman numerals is 3,999 which is MMMCMXCIX
Numbers larger than 3,999 in Roman numerals is represented using an overline. An overline on a Roman numeral means we are multiplying that Roman numeral by 1,000. For the number 50,000 in Roman numerals, you would use the Roman numeral L (50) with an overline to make it 50,000
Conversion Rules
I can be placed before V or X, represents subtract one, so IV (5-1) = 4 and 9 is IX (10-1)=9.
X can be placed before L or C represents subtract ten, so XL (50-10) = 40 and XC (100-10)=90.
C placed before D or M represents subtract hundred, so CD (500-100)=400 and CM (1000-100)=900. Roman numerals are usually written in highest to lowest from left to right except for some special cases where the left character is less than the right character.
Implementation
The algorithm is simple
1) Place the roman literals in an array
2) Place corresponding integer values (numbers) in another array maintaining the corresponding indices.
3) Try subtracting the biggest number from the numbers array from the input value until the input is 0.
4) Append the corresponding roman literal to output String.
ToRoman.java
The following class defines a method that accepts an integer and returns a Roman Number in the form of a String object.
/**
* I can be placed before V or X
*
* X can be placed before L or C
*
* C can be placed before D or M
*
*
*/
public static String convertToRomanNumber(int num) {
int number[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
StringBuilder result = new StringBuilder();
for (int i = 0; i < number.length; i++) {
while (num >= number[i]) {
num = num - number[i];
result = result.append(roman[i]);
}
}
return result.toString();
}
Console
System.out.println("Integer 4 : Roman " + RomanNumbers.convertToRomanNumber(4));
System.out.println("Integer 100 : Roman " + RomanNumbers.convertToRomanNumber(100));
System.out.println("Integer 2000 : Roman " + RomanNumbers.convertToRomanNumber(2000));
System.out.println("Integer 3,999 : Roman " + RomanNumbers.convertToRomanNumber(3999));
Integer 4 : Roman IV
Integer 100 : Roman C
Integer 2000 : Roman MM
Integer 3,999 : Roman MMMCMXCIX
This article shows how to print Even and Odd numbers alternatively using Java. It uses Two Java threads and a Class with two
methods to print even numbers and odd numbers respectively.
This article shows how to print Even and Odd numbers alternatively using Java. It uses Two Java threads and a Class with two
methods to print even numbers and odd numbers respectively.
What we are trying to achieve
We want to print even and odd numbers alternatively but using two threads. Essentially it is the interaction between two threads utilizing the wait() and notify() methods. One thread completes a task (printing a single even number) then it is put on wait state so that the other thread can get a chance to do its job (printing odd number) and this goes on until we are done with printing a set of numbers determined by max.
How it will work
Print Class (Print.java) with two synchronized methods even() and odd() which print even or odd numbers respectively.
Each method is synchronized so that two threads cannot execute them concurrently.
Each method first calls notifyAll() to so that other threads that are waiting can be wake up.
It then prints a number (even or odd) and then calls the wait() method on the current thread so it goes to waiting state.
Java Code
Print.java
This Class essentially has two synchronized methods. The constructor of this class takes a max number as a parameter. This max number is the limit to which the numbers will be printed.
It has two synchronized methods to print even and odd numbers alternatively.
class Print {
int max;
public Print(int max) {
this.max = max;
}
public synchronized void even() throws InterruptedException {
for (int i = 0; i <= max; i++) {
notifyAll();
if (i % 2 == 0)
System.out.println(Thread.currentThread().getName() + ":: " + i);
wait();
}
}
public synchronized void odd() throws InterruptedException {
for (int i = 0; i <= max; i++) {
notifyAll();
if (i % 2 != 0)
System.out.println(Thread.currentThread().getName() + ":: " + i);
wait();
}
}
}
Threads.java
Here we are creating an instance of Print class and creating two Threads.
From the first thread, we are calling print.even() method, from the second thread we are calling print.odd() method.
Print print = new Print(10);
//Thread to print even numbers
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
print.even();
} catch (InterruptedException e) {
}
}
});
//Thread to print odd numbers
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
print.odd();
} catch (InterruptedException e) {
}
}
});
t1.setName("Even Thread");
t2.setName(" Odd Thread");
t1.start();
t2.start();
Console Output
Even Thread:: 0
Odd Thread:: 1
Even Thread:: 2
Odd Thread:: 3
Even Thread:: 4
Odd Thread:: 5
Even Thread:: 6
Odd Thread:: 7
Even Thread:: 8
Odd Thread:: 9
Even Thread:: 10
Conclusion
From the odd() or even () method we have to first call notify or notifyAll() first.
Both odd() or even() method should be synchronized otherwise calling notifyAll() or wait() will throw exception
A thread in Java at any point of time exists in any one of the 6 states. This article give a quick introduction of different Thread states and respective methods which results in threads state change.
A thread in Java at any point of time exists in any one of the 6 states. This article give a quick introduction of different Thread states and respective methods which results in threads state change.
Following are the states of a Java Thread
New: Thre thread is just created by using start() method on the tread
Runnable: Thread state for a runnable thread. A thread in the runnable state is executing in the Java virtual machine but it may be waiting for other resources from the operating system such as a processor.
Blocked: A thread in the blocked state is waiting for a monitor lock
Waiting: A thread is in the waiting state due to calling one of the following methods: Object.wait() with no timeout Thread.join() with no timeout LockSupport.park
Timed Waiting: A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:Thread.sleep (long) Object.wait (long) Thread.join (long) LockSupport.parkNanos LockSupport.parkUntil
In this article, we will explore a few different ways to rotate a matrix clock by 90 degrees.
Rotating Matrix to 90 Clockwise
Following is our Matrix.
[123]
[456]
[789]
We will explore multiple ways to rotate this matrix clockwise 90
With Matrix Transpose
Matrix transpose is a flipped version of the matrix along its diagonal. In short, it is the same as interchanging the rows with columns.
For the above matrix the transposed matrix will look like.
[147]
[258]
[369]
Also, the transposed matrix is equivalent to 90 Left rotation of the original array.
RoateMatrixWithTranspose
Here the Rotation of the matrix is done in two steps
1) We transpose the matrix
2) And we interchange the columns
public void rotateMatrixRight90Transpose(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
for (int i = 0; i < m; i++) {
for (int j = i; j < n; j++) {
int x = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = x;
System.out.println("IC" + i + ":" + j);
}
}
// swap cols
for (int i = 0; i < m; i++) {
for (int j = 0; j < n / 2; j++) {
// swap mat[i][j] with mat[N-i-1][j]
int temp = mat[i][j];
mat[i][j] = mat[i][n - j - 1];
mat[i][n - j - 1] = temp;
}
}
}
Output
Matrix Rotation with Transpose
<= Original Matrix=>
[123]
[456]
[789]
<= After Transpose =>
[147]
[258]
[369]
<= After Rotation =>
[741]
[852]
[963]
Matrix Rotation in Single pass
MatrixRotation.java
The following code will rotate the matrix in a single pass.
public void rotate(int[][] matrix) {
int n = matrix.length;
for (int i = 0; i < (n + 1) / 2; i++) {
for (int j = 0; j < n / 2; j++) {
int temp = matrix[n - 1 - j][i];
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - j - 1];
matrix[n - 1 - i][n - j - 1] = matrix[j][n - 1 - i];
matrix[j][n - 1 - i] = matrix[i][j];
matrix[i][j] = temp;
}
}
}