Skip to content

Commit ade0240

Browse files
Examples
1 parent 03878cc commit ade0240

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.howtodoinjava.concurrency;
2+
3+
import java.security.SecureRandom;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.atomic.AtomicInteger;
7+
8+
public class SharedSeedExample {
9+
10+
private static final int THREAD_COUNT = 5;
11+
private static final int ITERATIONS_PER_THREAD = 10;
12+
private static ExecutorService executorService = Executors.newFixedThreadPool(THREAD_COUNT);
13+
14+
private static final AtomicInteger sharedCounter = new AtomicInteger(0);
15+
16+
public static void main(String[] args) {
17+
System.out.println("Using a shared Random instance:");
18+
java.util.Random sharedRandom = new java.util.Random();
19+
20+
for (int i = 0; i < THREAD_COUNT; i++) {
21+
executorService.submit(() -> {
22+
for (int j = 0; j < ITERATIONS_PER_THREAD; j++) {
23+
int randomNumber = sharedRandom.nextInt();
24+
processRandomNumber(randomNumber);
25+
}
26+
});
27+
}
28+
29+
System.out.println("\nUsing a shared SecureRandom instance:");
30+
SecureRandom sharedSecureRandom = new SecureRandom();
31+
32+
for (int i = 0; i < THREAD_COUNT; i++) {
33+
executorService.submit(() -> {
34+
for (int j = 0; j < ITERATIONS_PER_THREAD; j++) {
35+
int randomNumber = sharedSecureRandom.nextInt();
36+
processRandomNumber(randomNumber);
37+
}
38+
});
39+
}
40+
shutdownExecutor(executorService);
41+
}
42+
43+
private static void processRandomNumber(int randomNumber) {
44+
// Simulate some processing of the random number
45+
sharedCounter.incrementAndGet();
46+
}
47+
48+
private static void shutdownExecutor(ExecutorService executorService) {
49+
executorService.shutdown();
50+
while (!executorService.isTerminated()) {
51+
// Wait for all threads to finish
52+
}
53+
System.out.println(STR."The counter value: \{sharedCounter.get()}");
54+
}
55+
}

src/main/java/com/howtodoinjava/core/datetime/GetQuarterInfo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.time.format.DateTimeFormatter;
55
import java.time.temporal.IsoFields;
66
import java.time.temporal.TemporalAdjusters;
7+
import net.bytebuddy.asm.Advice.Local;
78

89
public class GetQuarterInfo {
910

@@ -23,5 +24,9 @@ public static void main(String[] args) {
2324
LocalDate lastDay = firstDay.plusMonths(2).with(TemporalAdjusters.lastDayOfMonth());
2425
System.out.println(firstDay);
2526
System.out.println(lastDay);
27+
28+
//Quarter between two dates
29+
long quarterCount = IsoFields.QUARTER_YEARS.between(firstDay, lastDay);
30+
System.out.println(quarterCount);
2631
}
2732
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.howtodoinjava.core.datetime;
2+
3+
import java.time.LocalDate;
4+
import java.time.temporal.ChronoUnit;
5+
6+
public class WeeksBetweenDates {
7+
8+
public static void main(String[] args) {
9+
LocalDate startLd = LocalDate.of(2023, 8, 5);
10+
LocalDate endLd = LocalDate.of(2023, 9, 5);
11+
long numWeeks = ChronoUnit.WEEKS.between(startLd, endLd);
12+
System.out.println(numWeeks);
13+
14+
long daysDifference = ChronoUnit.DAYS.between(startLd, endLd); // 31
15+
int extraDays = Long.valueOf(daysDifference % 7).intValue(); //3
16+
System.out.println(extraDays);
17+
}
18+
19+
}

src/main/java/com/howtodoinjava/jaxb/Main.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package com.howtodoinjava.jaxb;
22

3+
import com.howtodoinjava.xml.model.Department;
4+
import com.howtodoinjava.xml.model.Employee;
5+
import java.io.File;
6+
import java.io.StringWriter;
37
import java.util.HashMap;
48
import java.util.Map;
59

610
import javax.xml.bind.JAXBContext;
711
import javax.xml.bind.JAXBException;
12+
import javax.xml.bind.Marshaller;
813
import javax.xml.bind.annotation.XmlAccessType;
914
import javax.xml.bind.annotation.XmlAccessorType;
1015
import javax.xml.bind.annotation.XmlRootElement;
@@ -24,6 +29,16 @@ public static void main(final String[] args) throws JAXBException {
2429
JAXBContext context6 = getJAXBContext(Book.class);
2530
JAXBContext context7 = getJAXBContext(Book.class);
2631
JAXBContext context8 = getJAXBContext(Book.class);
32+
33+
JAXBContext jaxbContext = JAXBContext.newInstance( Employee.class );
34+
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
35+
36+
Employee employee = new Employee(1, "Lokesh", "Gupta", "India", new Department(101, "IT"));
37+
38+
//Overloaded methods to marshal to different outputs
39+
jaxbMarshaller.marshal(employee, System.out);
40+
jaxbMarshaller.marshal(employee, new File("output.xml"));
41+
jaxbMarshaller.marshal(employee, new StringWriter());
2742
}
2843

2944
@SuppressWarnings("rawtypes")
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.howtodoinjava.jaxb;
2+
3+
import com.howtodoinjava.xml.model.Employee;
4+
import jakarta.xml.bind.Unmarshaller;
5+
import java.io.File;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import jakarta.xml.bind.JAXBContext;
9+
import jakarta.xml.bind.JAXBException;
10+
import jakarta.xml.bind.Marshaller;
11+
import jakarta.xml.bind.annotation.XmlAccessType;
12+
import jakarta.xml.bind.annotation.XmlAccessorType;
13+
import jakarta.xml.bind.annotation.XmlElement;
14+
import jakarta.xml.bind.annotation.XmlRootElement;
15+
16+
public class MarshalListExample {
17+
18+
public static void main(String[] args) throws JAXBException {
19+
Employees employees = new Employees();
20+
21+
//Add the employees in list
22+
Employee emp1 = new Employee(1, "Lokesh", "Gupta", null, null);
23+
Employee emp2 = new Employee(1, "John", "McLean", null, null);
24+
employees.getEmployees().add(emp1);
25+
employees.getEmployees().add(emp2);
26+
27+
//Write to XML
28+
JAXBContext jaxbContext = JAXBContext.newInstance(Employees.class);
29+
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
30+
31+
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
32+
33+
//Marshal the employees list in console
34+
jaxbMarshaller.marshal(employees, System.out);
35+
36+
//Marshal the employees list in file
37+
jaxbMarshaller.marshal(employees, new File("out.xml"));
38+
39+
//unmarshal example
40+
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
41+
42+
//We had written this file in marshalling example
43+
Employees emps = (Employees) jaxbUnmarshaller.unmarshal(new File("out.xml"));
44+
45+
for (Employee emp : emps.getEmployees()) {
46+
47+
System.out.println(emp.getId());
48+
System.out.println(emp.getFirstName());
49+
}
50+
}
51+
}
52+
53+
@XmlRootElement(name = "employees")
54+
@XmlAccessorType(XmlAccessType.FIELD)
55+
class Employees {
56+
57+
@XmlElement(name = "employee")
58+
private List<Employee> employees = new ArrayList<>();
59+
60+
public List<Employee> getEmployees() {
61+
return employees;
62+
}
63+
64+
public void setEmployees(List<Employee> employees) {
65+
this.employees = employees;
66+
}
67+
}

0 commit comments

Comments
 (0)