LinkedBlockingQueue program does not terminate - java

If I run the following program, the JVM will not terminate after execution. However, if I uncomment the line (// newFixedThreadPool.execute(new Producer3());) from the code, the program terminates after execution. I am aware because of the blocking nature of the queue the program does not terminate. In the context of the below code what part of the code blocks the termination of the JVM?
public class LinkedBlockingQueueExample {
public static void main(String[] args) {
final BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<String>(5);
final class Producer implements Runnable {
#Override
public void run() {
try {
blockingQueue.put("Joshua");
blockingQueue.put("Bloch");
System.out.println("Put Joshua in the queue");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
final class Producer1 implements Runnable {
#Override
public void run() {
try {
blockingQueue.put("Martin");
blockingQueue.put("Fowler");
System.out.println("Put Mr Fowler in the Queue");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
final class Producer3 implements Runnable {
#Override
public void run() {
try {
blockingQueue.put("Malcom");
blockingQueue.put("Gladwell");
System.out.println("Put an Outlier in the Queue");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
final class Consumer implements Runnable {
#Override
public void run() {
try {
System.out.println(getClass() + " " + blockingQueue.take());
System.out.println(getClass() + " " + blockingQueue.take());
System.out.println(getClass() + " " + blockingQueue.take());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
final class Consumer1 implements Runnable {
#Override
public void run() {
try {
System.out.println(getClass() + " " + blockingQueue.take());
System.out.println(getClass() + " " + blockingQueue.take());
System.out.println(getClass() + " " + blockingQueue.take());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(5);
newFixedThreadPool.execute(new Producer());
newFixedThreadPool.execute(new Producer1());
// newFixedThreadPool.execute(new Producer3());
newFixedThreadPool.execute(new Consumer());
newFixedThreadPool.execute(new Consumer1());
newFixedThreadPool.shutdown();
}
}

Take() call is always blocking till elements become available..
if you don't want to block then user poll()
poll()
Retrieves and removes the head of this queue, or returns null if this queue is empty.
reference : http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingQueue.html#poll()

The extra "take" is blocking the termination. It blocks on the extra "take"

Related

Java mqtt concurrent connections is taking time

I am trying to make the concurrent session to MQTT server and all clients will disconnect once all the connections are made.
In the below publisher code I am trying to make concurrents sessions each sending 50 messages. And like this 500 threads will create and each will send 50 messages. But for creating 100 connections it is taking 10 minutes. Is there any mistake in coding and is it possible to decrease the rate of connection speed changing in below code, because the same thing I have written in Golang the rate of connection is high there.
Below is the publisher code:
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttSecurityException;
import org.eclipse.paho.client.mqttv3.MqttTopic;
public class Publisher extends Thread{
public static final String test_topic = "test";
private MqttClient client;
public static final String BROKER_URL = "tcp://192.168.1.129:1883";
CountDownLatch latch;
public Publisher(CountDownLatch latch) {
super();
this.latch = latch;
}
public void Publisher() {
String clientid=Thread.currentThread().getName();
System.out.println("=========== "+clientid);
MqttConnectOptions options = null;
try {
client = new MqttClient(BROKER_URL, clientid);
options = new MqttConnectOptions();
options.setCleanSession(true);
options.setMaxInflight(50);
client.connect(options);
} catch (MqttException e) {
try {
client.connect(options);
} catch (MqttSecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (MqttException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
//System.exit(1);
}
}
#Override
public void run() {
// TODO Auto-generated method stub
Publisher();
System.out.println(Thread.currentThread().getName());
try {
for(int i=0;i<50;i++)
{
//Thread.sleep(20);
publishTemperature();
}
} catch (MqttPersistenceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} /*catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
public void publishTemperature() throws MqttPersistenceException, MqttException {
final MqttTopic test = client.getTopic(test_topic);
final String temperature=""{\"test\":\"test\"}"";
test.publish(new MqttMessage(temperature.getBytes()));
//System.out.println("Published data. Topic: " + "test" + " Message: " + temperature);
}
public MqttClient getClient() {
return client;
}
public void setClient(MqttClient client) {
this.client = client;
}
}
Below is the main method:
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
public class test {
static Publisher[] Publisher=null;
public static void main(String[] args) throws MqttPersistenceException, MqttException, InterruptedException {
final CountDownLatch latch = new CountDownLatch(50);
Publisher = new Publisher[500];
for(int i=0;i<500;i++)
{
Thread.sleep(10);
Publisher[i]=new Publisher(latch);
Publisher[i].start();
}
latch.await();
for(int i=0;i<500;i++)
{
//Thread.sleep(10);
Publisher[i].getClient().disconnectForcibly(25);
}
}
}
Here all connections will connect and make persistent connection up to reaching 500 connections. After that, all connections will disconnect once.
Remove the following line:
Publisher[i].join();
The docs for Thread.join() say the following:
public final void join()
throws InterruptedException
Waits for this thread to die.
An invocation of this method behaves in exactly the same way as the
invocation
join(0)
This means that every time round the loop it will stop and wait for that thread to complete it's task before creating the new one.
If you remove that call it will allow all the threads to run in parallel.

Java Thread synchronized getCoon

I have a list of free sockets conn, and i put then in a Map , if status of free or inUse, but the synchronized dosent seens to be working correctly
this is my code:
my map is like this:
private ConcurrentHashMap<MVConnection, Boolean> listaConn = new ConcurrentHashMap<>();
my connManager is this:
public synchronized MVConnection getInstance() {
System.out.println("pass here on getInstance");
System.out.println("--------------------------------------------------------");
System.out.println("Before request Instance");
LogLinhas();
System.out.println("--------------------------------------------------------");
MVConnection searchResult = null;
System.out.println(Thread.currentThread().getName() + " is running");
while (searchResult == null) {
searchResult = this.listaConn.search(1, (conn, free) -> {
if (free) {
return conn;
}
return null;
});
}
notify();
System.out.println(Thread.currentThread().getName() + " notifying");
if (searchResult != null) {
this.listaConn.replace(searchResult, false);
try {
System.out.println("Set " + searchResult.getServerPort() + " as busy");
} catch (MVException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("--------------------------------------------------------");
System.out.println("After request Instance");
LogLinhas();
System.out.println("--------------------------------------------------------");
return searchResult;
}
and the function the request the cont is this:
public abstract class AbstractD3Dao<T extends Serializable> {
#Autowired
ConexaoD3 conexao;
protected MVConnection getCurrentSession() {
System.out.println("Abstract request the conn");
MVConnection connPool = null;
synchronized (conexao.getInstance()) {
while (connPool == null) {
try {
System.out.println("Aguardando Conexao");
System.out.println(Thread.currentThread().getName() + " is waiting");
wait();
} catch (InterruptedException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
connPool = conexao.getInstance();
}
}
try {
System.out.println(" Abstract assineg the PIB " + connPool.getServerPort() + "to use");
} catch (MVException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connPool;
}
`

Thread.join() not behaving as expected

I'm pretty new to Multithreading in java but am totally stumped about why this isn't behaving as I want it to.
I have a Producer-Consumer wherein I have
private void produceConsume() {
try {
Thread producer = new Thread(new Runnable() {
public void run() {
try {
produce();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
completedProduce = true;
}
}
private void produce() throws InterruptedException {
synchronized (this) {
while (queue.size() == capacity) {
wait();
}
try(InputStream is = new FileInputStream(file)) {
queue.add("hello");
} catch (IOException e) {
LOG.error("Error doing stream stuff: " + e.getMessage(), e);
}
notify();
}
}
});
producer.start();
List<Thread> consumers = new ArrayList<>();
for (int i = 0; i < noOfThreads; i++) {
Thread consumer = new Thread(new Runnable() {
#Override
public void run() {
try {
consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void consume() throws InterruptedException {
while (queue.size() > 0 || !completedProduce) {
synchronized (this) {
while (queue.size() == 0 && !completedProduce) {
wait();
}
String s = queue.poll();
System.out.println(s);
}
notify();
}
}
}
});
consumer.start();
consumers.add(consumer);
}
for (Thread t : consumers) {
t.join();
}
producer.join();
} catch (Exception e) {
LOG.error("InterruptedException e: " + e.getMessage(), e);
} finally {
LOG.info("We are done with this file!");
}
}
Now, I've noticed that all functionality changes based on where I put my producer.join() statement. For example, if I put producer.join() right after producer.start() then everything works - but the number of threads has no impact on runtime. This makes sense as I'm slowed down drastically by how long it takes to produce and so the longest task wins out.
However, if I put producer.join() where it is in the example provided (I do the join when I do the join for the consumers) then everything just stops running before the producer actually finishes. As in, the program stalls after the first thing is consumed, waiting for something, but the thread never dies.
How do I make it so that things run correctly and nothing stalls waiting for another process to finish?
Thanks in advance,

exception handling in executor service

When I throw exception from student name archana.
As per my understanding InvokeAll waits for all task to be completed and then return future list
Output I get is
pool-1-thread-1 Helloprerna
pool-1-thread-2 Helloabc
HELLO SOMEERROR
Execution Completed
I want other tasks output to be show for which exception is not thrown.Any suggestions
public class Executor {
public static void main(String args[]) throws InterruptedException{
ExecutorService executor=Executors.newFixedThreadPool(5);
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("prerna"));
list.add(new Student("abc"));
list.add(new Student("archana"));
list.add(new Student("def"));
list.add(new Student("xyz"));
list.add(new Student("ritu"));
list.add(new Student("babita"));
try {
List<Future<String>> resultList=executor.invokeAll(list);
for(Future<String> f:resultList){
//if(f.isDone()){
System.out.println(f.get());
//}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ExecutionException e) {
// TODO Auto-generated catch block
System.out.println("HELLO SOME ERROR");
//e.printStackTrace();
}
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
System.out.println("Execution Completed");
}
}
.
public class Student implements Callable<String>{
String name;
public Student(String name) {
super();
this.name = name;
}
#Override
public String call() throws Exception {
// TODO Auto-generated method stub
if(name=="archana"){
throw new Exception();
}
return display(name);
}
private String display(String name2) {
try {
// System.out.println(Thread.currentThread().getName());
name2=Thread.currentThread().getName()+" Hello"+ name;
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return name2;
}
}
You can move around the try/catch:
Original:
try {
List<Future<String>> resultList=executor.invokeAll(list);
for(Future<String> f:resultList){
// if(f.isDone()){
System.out.println(f.get());
//}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ExecutionException e) {
// TODO Auto-generated catch block
System.out.println("HELLO SOME ERROR");
// e.printStackTrace();
}
will be rather:
try {
List<Future<String>> resultList=executor.invokeAll(list);
for(Future<String> f:resultList){
try{
System.out.println(f.get());
}catch (ExecutionException e) {
System.out.println("HELLO SOME ERROR: " + e.getMessage());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
So here you will get all OK results and you can handle the exceptional execution for each task.
The pattern for this should be: Main thread create and call slaves threads, which should return ok value or error value (if there was any error). Then Main thread should collect results from slaves and process them.

Using addShutdownHook in java

I want to do following operation in ordered wise
1. Stop jetty server
2. Delete used resource from jetty
3. Restart jetty server.
I have done this above using shutdownhook in java as below :
<code>
Thread restartThread = new Thread(){
public void run(){
try {
sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String osName = System.getProperty("os.name");
logger.debug("OS name:" + osName);
if (osName != null
&& osName.toUpperCase().startsWith("WINDOWS")) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
List<String> cmdLine = new ArrayList<String>();
cmdLine.add("cmd.exe");
cmdLine.add("/C");
cmdLine.add("start");
cmdLine.add("\"\"");
cmdLine.add(getBaseDir() + File.separator + "restart.bat");
final ProcessBuilder pb = new ProcessBuilder(cmdLine);
Process process = pb.start();
if (process.exitValue() == 0) {
// after stopping server delete stores
deleteCertificates();
// restores files
restoreFiles(tmpdir, backupfilelist);
}
//p.waitFor();
} catch (IOException e) {
logger.error("Failed to restart:" + e.getMessage(), e);
}
}
});
System.exit(0);
} else {
try {
Runtime.getRuntime().exec("service appservice restart");
} catch (IOException e) {
logger.error("Failed to restart:" + e.getMessage(), e);
}
}
}
};
restartThread.start();
<code>
my concern is will it do it sequentially execution, otherwise application will fail to restore.

Categories