Running “apt-get update” ubuntu complains about missings ppa keys.

In my case I was trying to install Libre Office and the missing key was 83FBA1751378B444.

I couldn´t find any key running:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 83FBA1751378B444

The message that I gog in the console was:
gpg: requesting key 83FBA1751378B444 from hkp server keyserver.ubuntu.com
gpgkeys: key 83FBA1751378B444 not found on keyserver
gpg: no valid OpenPGP data found.
gpg: Total number processed: 0

The solution to me was running:

 sudo launchpad-getkeys 

and the I could finally run “apt-get update”

The Comparable interface allows you to sort by a specific object variable that you choose for your needs.
Take a look at the following object:

public class Book implements Comparable<Book> {
	
	private String title;
	private String author;
	private double price;
	
	public Book(String title, String author, double price) {
		super();
		this.title = title;
		this.author = author;
		this.price = price;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}


	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}


	@Override
	public int compareTo(Book that) {
		return Double.valueOf(this.price).compareTo(Double.valueOf(that.price));
	}

	public String toString() {
		return " \n " + title + " \t " + author + " \t " + price;
		}
		
}

Notice the way the compareTo method is overriden. It´s a double primitive variable, but you need the wrapper class to compare it. The following wouldn´t work:

	@Override
	public int compareTo(Book that) {
		return this.price.compareTo(that.price);
		return 0;
	}

Implementing the Comparable interface affects the sorting criteria. For example:

import java.util.Arrays;

public class ComparableTest{
	
	public static void main(String[] args) {
		
		Book[] books= {
		new Book("The capital","Marx,Karl",10.5),
		new Book("Interpretation of dreams","Freud, S.",12.5),
		new Book("Killed by Death","Kilmister, Lemmy",8)
		};
		
		System.out.println("Before sorting: \n"+Arrays.toString(books));

	Arrays.sort(books);
	System.out.println("\n After sorting: \n"+Arrays.toString(books));
	
	}
	
}

The output is:

Before sorting:
[
The capital Marx,Karl 10.5,
Interpretation of dreams Freud, S. 12.5,
Killed by Death Kilmister, Lemmy 8.0]


After sorting:
[
Killed by Death Kilmister, Lemmy 8.0,
The capital Marx,Karl 10.5,
Interpretation of dreams Freud, S. 12.5]

If you need to do a more complex comparison, you can make your own comparator by extending the Comparator interface (not “Comparable”).
For example you can create your own Comparator:

public class BookComparator implements Comparator<Book> {

	@Override
	public int compare(Book b1, Book b2) {
		
		int comparePrice = Double.valueOf(b1.getPrice()).compareTo(Double.valueOf(b2.getPrice()));
		if (comparePrice!=0) 
			{return comparePrice;
			}
		else return b1.getAuthor().compareTo(b2.getAuthor());
	}

}

It tries to order by price first, but ifthe books have the same price, they will be sorted by the author name too.

If you runt the following main method:

	public static void main(String[] args) {

		ArrayList<Book> booksList = new ArrayList<Book>();
		booksList.add(new Book("The capital", "Marx,Karl", 10.5));
		booksList.add(new Book("Peace and War", "Tostoj. L.", 12.5));
		booksList.add(new Book("Interpretation of dreams", "Freud, S.", 12.5));
		booksList.add(new Book("Killed by Death", "Kilmister, Lemmy", 8));
		
		System.out.println("before sorting:");
		for (Book book : booksList) {
			 System.out.println( book.toString());

		}

		Collections.sort(booksList, new BookComparator());
		System.out.println(""
				+ "\n after sorting:");
		
		for (Book book : booksList) {
			 System.out.println(book.toString());

		}

Then you see the following output:

before sorting:
The capital Marx,Karl 10.5
Peace and War Tostoj. L. 12.5
Interpretation of dreams Freud, S. 12.5
Killed by Death Kilmister, Lemmy 8.0

after sorting:
Killed by Death Kilmister, Lemmy 8.0
The capital Marx,Karl 10.5
Interpretation of dreams Freud, S. 12.5
Peace and War Tostoj. L. 12.5

Notice the order of the last two books.

To learn about Java threads and concurrency I have decided to represent a Call Center. The customers making the phone calls to a call center can be represented as threads.
To simulate the real world scenario I will use Semaphores and a Cyclic Barrier to simulate the standby situation.

It´s a simple Producer/Consumer scenario. The producers are the operators working in a call centers.
The critical block is the phone call, simulated with a thread sleep method.

First of all let´s start with semaphores.

Semaphores can control access to more than one resource at a time. They enable a certain amount of threads in a program block.
In the semaphore constructor you can specify the number of threads that can access the resource at the same time.
With the method acquire() the thread tries to get the resource. If the resource is busy, the thread will wait.
The release() method frees the resource from the thread.

CyclicBarrier offers an elegant way to trigger an event if all the operators are busy.

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;

public class Customer extends Thread {

	CyclicBarrier standByMessage;
	private Semaphore operators;

	public Customer(Semaphore operators, CyclicBarrier standByMessage,
			String name) {
		this.standByMessage = standByMessage;
		this.operators = operators;
		this.setName(name);
		this.start();
	}

	@Override
	public void run() {

		// simulate the time required for the phone call (between one and six seconds...
		long duration = ThreadLocalRandom.current().nextLong(1,10);

		try {

			System.out.println(getName()
					+ " is waiting to speak to the operator...");

			standByMessage.await();
			// if you put acquire first and then await the operators are not
			// free and all the customers are not called

			operators.acquire();

			System.out.println(getName()
					+ " is getting the connection to the operator ...");
                        //using TimeUnit enumeration to make the code more readable
			Thread.sleep(TimeUnit.SECONDS.toMillis(duration));
			
			System.out.println(getName()
					+ "´s phone call with the operator ending.");
			operators.release();
			
			System.out.println("Available operators="
					+ operators.availablePermits());
			
		} catch (InterruptedException | BrokenBarrierException e) {
			System.err.println(e);
		} 

	}
}

Take a look at the way the duration of the call is generated. I am using the ThreadLocalRandom class, that in a multithreading context it´s helpful to get the job done much faster.
At first I tried it with Math.random() and it was MUCH slower!

The call center has 3 operators:

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;

public class CallCenter {

	public static void main(String[] args) {
		
		Semaphore operators = new Semaphore(3);
		
		CyclicBarrier standbyMessage = new CyclicBarrier(3, new StandBy());
	
		System.out.println("The call center is ready to answer phone calls");
		
        System.out.println("Available operators initially=" + operators.availablePermits() );
        
		new Customer(operators,standbyMessage,"Laura");
		new Customer(operators,standbyMessage,"Mario");
		new Customer(operators,standbyMessage,"Luigi");
		new Customer(operators,standbyMessage,"Paola");
		new Customer(operators,standbyMessage,"Alfonso");
		new Customer(operators,standbyMessage,"Anna");
		new Customer(operators,standbyMessage,"Giorgio");
		new Customer(operators,standbyMessage,"Francesca");
		new Customer(operators,standbyMessage,"Pietro");
		new Customer(operators,standbyMessage,"Antonio");
		new Customer(operators,standbyMessage,"Marco");
		new Customer(operators,standbyMessage,"Giovanna");
		new Customer(operators,standbyMessage,"Daniele");
		new Customer(operators,standbyMessage,"Giorgio");
	}

}

The Cyclic barrier takes a Thread as argument for the constructor:

public class StandBy implements Runnable {

	@Override
	public void run() {
    System.out.println("All operators are busy at the moment - playing MUSIC ...");
	}

}

The output is something like:

The call center is ready to answer phone calls
Available operators initially=3
Alfonso is waiting to speak to the operator...
Laura is waiting to speak to the operator...
Paola is waiting to speak to the operator...
Luigi is waiting to speak to the operator...
Anna is waiting to speak to the operator...
Mario is waiting to speak to the operator...
Giorgio is waiting to speak to the operator...
All operators are busy at the moment - playing MUSIC ...
Francesca is waiting to speak to the operator...
Pietro is waiting to speak to the operator...
All operators are busy at the moment - playing MUSIC ...
Paola is getting the connection to the operator ...
Giovanna is waiting to speak to the operator...
Luigi is getting the connection to the operator ...
Marco is waiting to speak to the operator...
Antonio is waiting to speak to the operator...
Giorgio is waiting to speak to the operator...
All operators are busy at the moment - playing MUSIC ...
Daniele is waiting to speak to the operator...
Mario is getting the connection to the operator ...
All operators are busy at the moment - playing MUSIC ...
Luigi´s phone call with the operator ending.
Available operators=1
Alfonso is getting the connection to the operator ...
Mario´s phone call with the operator ending.
Available operators=1
Laura is getting the connection to the operator ...
Paola´s phone call with the operator ending.
Available operators=1
Francesca is getting the connection to the operator ...
Alfonso´s phone call with the operator ending.
Available operators=1
Pietro is getting the connection to the operator ...
Laura´s phone call with the operator ending.
Available operators=1
Marco is getting the connection to the operator ...
Francesca´s phone call with the operator ending.
Available operators=1
Anna is getting the connection to the operator ...
Pietro´s phone call with the operator ending.
Available operators=1
Giovanna is getting the connection to the operator ...
Anna´s phone call with the operator ending.
Available operators=1
Daniele is getting the connection to the operator ...
Marco´s phone call with the operator ending.
Available operators=1
Giorgio is getting the connection to the operator ...
Giorgio´s phone call with the operator ending.
Available operators=1
Giovanna´s phone call with the operator ending.
Available operators=2
Daniele´s phone call with the operator ending.
Available operators=3

The thread class provides some methods to define the threads behavior.
Two important methods to consider are:
join(), to make the other instanced threads to wait for it to day. You can provide a timeout as parameter.
sleep();

The sleep() method throws a checked exception called “InterruptedException”,each time the thread receives an interrupt request. You have to handle in the code by wrapping the method in a try-catch block.

The following code will make you understand the join method.
We define a MyThread thread:

public class MyThread implements Runnable {

	public void run() {
		Thread thread= Thread.currentThread();
		System.out.println("Implementing the runnable interface"
				+ " - Thread name: " + thread.getName()
				+ " - priority: " + thread.getPriority()
				+ " - group: " + thread.getThreadGroup().getName());

	}

}

Then we can observe the behaviour running the following main:

		public static void main(String[] args) {

		// extending Thread
		Thread myThread = new Thread(new MyThread());
		Thread myJoiningThread = new Thread(new MyThread());

		myThread.setName("myThread");
		myJoiningThread.setName("myJoiningThread");

		System.out.println("Threads will be started...");

		myJoiningThread.start();

		try {
			// waits for this thread to die
			myJoiningThread.join();
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		myThread.start();

	}

Without the try-catch block in which the join method is invoked the threads would be started asynchronously (the order is not predictable) and in the console you might get either:
Threads will be started...
Implementing the runnable interface - Thread name: myThread - priority: 5 - group: main
Implementing the runnable interface - Thread name: myJoiningThread - priority: 5 - group: main

or:

Threads will be started...
Implementing the runnable interface - Thread name: myJoiningThread - priority: 5 - group: main
Implementing the runnable interface - Thread name: myThread - priority: 5 - group: main

But if you include the try-catch block, the myJoiningThread will always be invoked first.

To understand the sleep method you can create a MySleepingThread:

public class MySleepingThread implements Runnable {

	public void run() {
		Thread thread= Thread.currentThread();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Implementing the runnable interface"
				+ " - Thread name: " + thread.getName()
				+ " - priority: " + thread.getPriority()
				+ " - group: " + thread.getThreadGroup().getName());

	}

}

Then you can run the following main method:

public static void main(String[] args) {

		// extending Thread
		Thread myThread = new Thread(new MyThread());
		Thread mySleepingThread = new Thread(new MySleepingThread());
		myThread.setName("myThread");
		mySleepingThread.setName("mySleepingThread");

		System.out.println("Threads will be started...");

		mySleepingThread.start();
		myThread.start();

	}

You can see in the console that the sleeping thread will be started after 3 seconds.

The sleep method is static. By invoking “Thread.sleep(3000)” you are delaying the creation of the current thread.

If you use the method in the main method, the sleeping thread will be “main”.

I am learning for the Java OCP 7 and summarizing some relevant infos about Multithreading.
For now I am referring to chapter 13 in the book by G.Ganesh.

You can create threads in 2 ways: extending the Thread class or implementing the Runnable interface
Extending the Thread class is more convenient if you don´t need to extend another class that is not “Thread” (since in java you can only extend one class), because you can use the class methods directly (like “getName()“).
If you implement the Runnable interface (preferred way because of inheritance) you will have to use the static method “currentThread()” of the Thread class to do your operations on the thread (like setting or getting the name).

The two fundamental methods to know are:
run(): like a main method, necessary to enable the execution of a thread.
start(): that creates the thread.

The thread will be terminated when the run method execution is complete. It´s invoked implicitly by the JVM (DON´T invoke it explicitly!).
The main method starts a Thread called “main“. If you create and start your own thread it will have default names like “Thread-0“, “Thread-1“.

If you extend the thread class you need to override the run() method (otherwise it does nothing). If you implement the Runnable interface you will have to implement the run() method (mandatory)

The following code will help you to understand more about these first concepts.

public class MyThread1 extends Thread {

	public void run() {
		System.out.println("Extending the Thread class - Thread name: "
				+ getName() + " - priority: " + getPriority() + " - group: "
				+ getThreadGroup().getName());

	}

}
public class MyThread2 implements Runnable {

	public void run() {
		Thread thread= Thread.currentThread();
		System.out.println("Implementing the runnable interface"
				+ " - Thread name: " + thread.getName()
				+ " - priority: " + thread.getPriority()
				+ " - group: " + thread.getThreadGroup().getName());

	}

}
public class ThreadsDemo {

	public static void main(String[] args) {

		// extending Thread
		Thread myThread1 = new MyThread1();
		// implementing Runnable
		Thread myThread2 = new Thread(new MyThread2());

		System.out.println("Threads defined. Default properties: ");
		//name, priority, group returned by the toString method 
		System.out.println(myThread1);
		System.out.println(myThread2);
		System.out.println("Changing default names...");
		myThread1.setName("Thread_1");
		myThread2.setName("Thread_2");
		System.out.println("Threads will be started...");
		myThread1.start();
		myThread2.start();

	}

}

The output of the main method should be something like:

Threads defined. Default properties:
Thread[Thread-0,5,main]
Thread[Thread-1,5,main]
Threads will be started...
Extending the Thread class - Thread name: Thread-0 - priority: 5 - group: main
Implementing the runnable interface - Thread name: Thread-1 - priority: 5 - group: main

Launching the main program you see that the threads are started asynchronously. Sometimes you see:
Implementing the runnable interface - Thread name: Thread-1
Extending the Thread class - Thread name: Thread-0

Sometimes it´s:
Extending the Thread class - Thread name: Thread-0
Implementing the runnable interface - Thread name: Thread-1

You can quickly proof it in Eclipse launching the program with the shortcut Ctlr+11.

To change this “random” behaviour you can set the priority with the setPriority() method, from 1, the lowest, to 10, the highest. The default is 5. They can be all retrieved by the static members:
– Thread.NORM_PRIORITY ;
– Thread.MAX_PRIORITY ;
– Thread.MIN_PRIORITY .

Threads can be in one of the following states:
– NEW (created)
– RUNNABLE (started)
– TERMINATED
– BLOCKED (waiting to acquire the lock)
– WAITING (waiting for notifications)
– TIMED_WAITING (sleep() invoked and the thread is sleeping or if wait() with timeout)

States are defined with the Thread.State enumeration.
You can get the state with the getState() method.

If you need to convert audio files in another format you can use the tool called “soundconverter”, that you can simply install:

~$ sudo apt-get install soundconverter

Then simply run a command like:

~$ soundconverter -b -m "audio/mpeg" -s ".mp3" /home/laura/cd-wav/*.wav

“-b” stands for batch type

“-m” to specify the mime format.

“-s” to specify the suffix

And move them into another folder eventually:

~$ mv /home/laura/cd-irene/*.mp3 /home/laura/cd-wav.

For more info check the official wiki pages:
http://wiki.ubuntuusers.de/soundconverter
http://manpages.ubuntu.com/manpages/karmic/man1/soundconverter.1.html

You can ping instances that are in the same security group by connecting through ssh using the internal IP:
ping 10.0.0.15

In some posts and forum I read about adding custom ICMP rule in the security group. However it´s not what it takes, a least for me.
But the following rule will work:
Type: All ICMP
Protocol: TCP
Port range: 0 – 65535
Source: Anywhere – 0.0.0.0/0

After doing this you will be able to ping other instances. You should see something like:

PING 10.0.0.15 (10.0.0.15): 56 data bytes
64 bytes from 10.0.0.14: icmp_seq=1 ttl=64 time=3.9 ms
64 bytes from 10.0.0.14: icmp_seq=2 ttl=64 time=3.9 ms
64 bytes from 10.0.0.14: icmp_seq=3 ttl=64 time=10.6 ms
64 bytes from 10.0.0.14: icmp_seq=4 ttl=64 time=40.6 ms
64 bytes from 10.0.0.14: icmp_seq=5 ttl=64 time=3.8 ms
64 bytes from 10.0.0.14: icmp_seq=6 ttl=64 time=5.3 ms
64 bytes from 10.0.0.14: icmp_seq=7 ttl=64 time=6.5 ms
64 bytes from 10.0.0.14: icmp_seq=8 ttl=64 time=3.5 ms
64 bytes from 10.0.0.14: icmp_seq=9 ttl=64 time=21.0 ms
64 bytes from 10.0.0.14: icmp_seq=10 ttl=64 time=3.5 ms
64 bytes from 10.0.0.14: icmp_seq=11 ttl=64 time=3.5 ms
64 bytes from 10.0.0.14: icmp_seq=12 ttl=64 time=59.7 ms
64 bytes from 10.0.0.14: icmp_seq=13 ttl=64 time=3.5 ms
64 bytes from 10.0.0.14: icmp_seq=14 ttl=64 time=3.5 ms
64 bytes from 10.0.0.14: icmp_seq=15 ttl=64 time=4.8 ms
64 bytes from 10.0.0.14: icmp_seq=16 ttl=64 time=3.1 ms
64 bytes from 10.0.0.14: icmp_seq=17 ttl=64 time=3.1 ms
64 bytes from 10.0.0.14: icmp_seq=18 ttl=64 time=3.0 ms
64 bytes from 10.0.0.14: icmp_seq=19 ttl=64 time=3.1 ms

--- 10.0.0.14 ping statistics ---
20 packets transmitted, 19 packets received, 5% packet loss
round-trip min/avg/max = 3.0/9.9/59.7 ms

That´s it.

I was dealing with the Apache modcluster configuration for the Wildfly load balancing and I ran into the following error

[Server:server-four] 16:55:00,093 ERROR [org.jboss.modcluster] (UndertowEventHandlerAdapter - 1) MODCLUSTER000042: Error MEM sending STATUS command to localhost/127.0.0.1:6666, configuration will be reset: MEM: Can't read node

The error occurs when the servers are “idle”, that is where there is nore request from the browser for a time longer than the keep alive timeout setting.

Oracle already provides an automatic exclusive locking mechanism for the rows you want to update. However you can override it to customize the performance (for example, if you need consistent data and/or exclusive access for the duration of a whole and more complex transaction).

When your application requires consistent data for the duration of the transaction, not reflecting changes by other transactions, you can achieve transaction-level read consistency by using explicit locking, read-only transactions, serializable transactions, or by overriding default locking.

Row locking at transactional level can be achieved with “SELECT FOR UPDATE” statement. The lock is released after a commit or rollback only.

If you want to lock single rows in Oracle PL/SQL you can use cursors, pointing at the rows you want to lock.

The following example show you how to lock single rows:

CREATE OR REPLACE PROCEDURE LOCK_ORDER_ENTRY 
(id_number IN number, system_user IN varchar2)
IS
/*"order_row" is a variable to store the row found by the cursor select statement   */
order_row order_queue%rowtype;
cursor c1 is
    SELECT * 
    FROM order_queue 
    WHERE id=id_number
    FOR UPDATE NOWAIT;
BEGIN
/* first of all you need to open the cursor */
OPEN c1;
/* then you need to fetch the content in the variable */
   LOOP
      FETCH c1 INTO order_row;
/* the lock will be released after the commit */
      IF (c1%found) THEN
         UPDATE order_queue SET processed=1, user=system_user where CURRENT OF c1;
         COMMIT; 
      END IF;
 /* then you need close the cursor */
  END LOOP;
CLOSE c1;
END LOCK_ORDER_ENTRY;

The “select for update” statement has two possible variants
– FOR UPDATE NO WAIT, that triggers an error if the row is locked by another user.
– FOR UPDATE SKIP LOCKED, that fastens the execution by skipping the already locked rows

If you need to lock a single row in the database you don´t need a loop.

To install apache on Windows, you can download the binary from:
http://www.apachehaus.com/cgi-bin/download.plx?dli=gUEJEMjNVWx4EVV9yUsZVTJVlUGRVYSFlUuB3T

Then you need to edit the httpd.conf file to change the path to the server root (SRVROOT). It should be something like:
Define SRVROOT “C:\Users\laura\httpd-2.4.12-x86-r2\Apache24″

If you are running Skype (that listens on the port 80), there will be a conflict In this case you need either to kill Skype or change the default Apache listening port in the httpd.conf

If you need to enable apache modules (if no yet enabled) that you might need for your activities you need the “a2enmod” command.

There are several solutions that you can fidn by googling, like:
http://aninternetpresence.net/program/
https://github.com/enderandpeter/win-a2enmod