Skip to content

Commit fe86664

Browse files
committed
added threads
1 parent 7ad3588 commit fe86664

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

Linkedlist.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.*;
2+
3+
public class Linkedlist {
4+
public static void main(String[] args) {
5+
LinkedList<String> list = new LinkedList<String>();
6+
// LinkedList<String> list2 = {"This", "is", "a", "linked", "list"};
7+
8+
list.add("Hello");
9+
list.add("World");
10+
list.add("Java");
11+
12+
System.out.println("Printing the linked list: " + list);
13+
System.out.println("Size of the linked list: " + list.size());
14+
System.out.println("Element at index 1: " + list.get(1));
15+
list.set(2, "Python");
16+
list.addFirst("C++"); // Adds element at the beginning of the list
17+
list.addLast("C#"); // Adds element at the end of the list
18+
list.removeFirst(); // Removes the first occurrence of the element
19+
list.add("Ravi");
20+
list.add("Vijay");
21+
list.add("Ajay");
22+
list.add("Anuj");
23+
list.removeLast(); // Removes the last occurrence of the element
24+
list.add("Gaurav");
25+
list.add("Harsh");
26+
list.remove(1); // Removes the element at index 1
27+
list.add("Virat");
28+
list.add("Gaurav");
29+
list.add("Harsh");
30+
list.add("Amit");
31+
// list.addAll(list2);
32+
System.out.println("Printing the linked list: " + list);
33+
}
34+
}

MathFunc.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package mathfuncs;
2+
3+
// It is a package for storing all the math related functions
4+
5+
public class MathFunc {
6+
public int add(int a, int b) {
7+
return a + b;
8+
}
9+
10+
public int subtract(int a, int b) {
11+
return a - b;
12+
}
13+
14+
public int multiply(int a, int b) {
15+
return a * b;
16+
}
17+
18+
public int divide(int a, int b) {
19+
return a / b;
20+
}
21+
22+
public int modulus(int a, int b) {
23+
return a % b;
24+
}
25+
26+
public int increment(int a) {
27+
return a++;
28+
}
29+
30+
public int decrement(int a) {
31+
return a--;
32+
}
33+
34+
public int unaryMinus(int a) {
35+
return -a;
36+
}
37+
}

This.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import java.util.*;
22

3+
import mathfuncs.*;
34
// Understanding 'this' keyword
45

56
public class This {
@@ -17,6 +18,8 @@ public static void main(String[] args) {
1718
This obj1 = new This();
1819
This obj2 = new This(30);
1920

21+
System.out.println("Age value: " + MathFunc.add(10, 20));
22+
2023
System.out.println("Age value: " + age);
2124
System.out.println("Age value: " + obj1.age);
2225
System.out.println("Age value: " + obj2.age);

Threads.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.*;
2+
3+
// Understanding Threads
4+
// Threads are independent paths of execution in a program, they can be used to perform tasks in parallel.
5+
// The life cycle of a thread in Java is controlled by the JVM. The thread states are:
6+
// 1. New: A new thread is created but not started.
7+
// 2. Runnable: A thread is ready to run and waiting for the JVM to schedule it.
8+
// 3. Running: The thread is being executed.
9+
// 4. Blocked: The thread is waiting for a monitor lock to enter a synchronized block/method.
10+
// 5. Waiting: The thread is waiting for another thread to perform a particular action.
11+
// 6. Timed Waiting: The thread is waiting for another thread to perform an action for up to a specified waiting time.
12+
// 7. Terminated: The thread has completed its execution.
13+
// To stop the main method excution, we can use the join() method. The join() method waits for the threads to die.
14+
// The synchronized keyword ensures that only one thread of the program executes at a time, blocking the excution of the other thread.
15+
16+
class Counter {
17+
int count = 0;
18+
19+
public synchronized void count() {
20+
count += 1;
21+
}
22+
}
23+
24+
public class Threads {
25+
public static void main(String[] args) throws InterruptedException {
26+
Counter c = new Counter();
27+
28+
Runnable hi = () -> {
29+
for (int i = 0; i < 10; i++) {
30+
c.count();
31+
}
32+
};
33+
Runnable hello = () -> {
34+
for (int i = 0; i < 10; i++) {
35+
c.count();
36+
}
37+
};
38+
39+
Thread t1 = new Thread(hi);
40+
Thread t2 = new Thread(hello);
41+
42+
t1.start();
43+
t2.start();
44+
45+
t1.join();
46+
t2.join();
47+
48+
System.out.println("Count: " + c.count);
49+
}
50+
}

0 commit comments

Comments
 (0)