https://www.gatevidyalay.com/concurrency-problems-in-transaction/
Saturday, 29 December 2018
Implement ThreadPool in java
public class Test {
public static void main(String[] args) {
ThreadPool threadPool = new ThreadPool(2);
for(int i=0;i<2;i++) {
Task task = new Task(i);
threadPool.execute(task);
}
System.out.println("done");
}
}
public class Task implements Runnable{
int i;
public Task(int i) {
this.i = i;
}
@Override
public void run() {
System.out.println("executing i::"+i);
}
}
public class ThreadPool {
int nThread;
private LinkedBlockingQueue<Runnable> queue;
PoolWorker[] arr ;
public ThreadPool(int nThread) {
this.nThread = nThread;
this.queue = new LinkedBlockingQueue<>();
arr = new PoolWorker[nThread];
for(int i=0;i<nThread;i++) {
PoolWorker poolWorker = new PoolWorker();
arr[i] = poolWorker;
arr[i].start();
}
}
public void execute(Task task) {
synchronized (queue) {
queue.add(task);
queue.notify();
}
}
class PoolWorker extends Thread{
@Override
public void run() {
Runnable task;
while(true) {
synchronized (queue) {
while(queue.isEmpty()) {
try {
queue.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
task = queue.poll();
}
task.run();
}
}
}
}
Ref: https://www.javacodegeeks.com/2016/12/implement-thread-pool-java.html
public static void main(String[] args) {
ThreadPool threadPool = new ThreadPool(2);
for(int i=0;i<2;i++) {
Task task = new Task(i);
threadPool.execute(task);
}
System.out.println("done");
}
}
public class Task implements Runnable{
int i;
public Task(int i) {
this.i = i;
}
@Override
public void run() {
System.out.println("executing i::"+i);
}
}
public class ThreadPool {
int nThread;
private LinkedBlockingQueue<Runnable> queue;
PoolWorker[] arr ;
public ThreadPool(int nThread) {
this.nThread = nThread;
this.queue = new LinkedBlockingQueue<>();
arr = new PoolWorker[nThread];
for(int i=0;i<nThread;i++) {
PoolWorker poolWorker = new PoolWorker();
arr[i] = poolWorker;
arr[i].start();
}
}
public void execute(Task task) {
synchronized (queue) {
queue.add(task);
queue.notify();
}
}
class PoolWorker extends Thread{
@Override
public void run() {
Runnable task;
while(true) {
synchronized (queue) {
while(queue.isEmpty()) {
try {
queue.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
task = queue.poll();
}
task.run();
}
}
}
}
Ref: https://www.javacodegeeks.com/2016/12/implement-thread-pool-java.html
Thursday, 6 December 2018
What does start() function do in multithreading in Java?
The purpose of start() is to create a separate call stack for
the thread. A separate call stack is created by it, and then run() is
called by JVM.
Ref: https://www.geeksforgeeks.org/start-function-multithreading-java/
Ref: https://www.geeksforgeeks.org/start-function-multithreading-java/
Subscribe to:
Posts (Atom)
links for Data Structure
1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭: https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...
-
Sample Code: Running code for add text water mark on PDF in java using iText , the water mark drawing on center horizontally. import ...
-
import org.springframework.data.repository.NoRepositoryBean; import org.springframework.data.repository.Repository; import java...
-
Step1: reach to project directory where pom.xml file exist from terminal Step2: clean mavan using below mvn clean Step3: create a war f...