There could be a want for an application that performs different functionality, completing multiple operations at the same time. The JNIOR accomplishes this with threading. A thread is a sequence of instructions within a program that can be executed independently of other code. The JNIOR is able to handle threading via the default JAVA library. Below are two quick examples of threading, with one of them using synchronization.
In this first example, there is a main method that creates two threads, each supplying it an array of words that will be used to create sentences and print them out. Each new thread calls a function called slowStringBuffer
, taking the words in their string array’s they’ve been supplied and adding them to the string buffer in that function. Since the threads run at the same time, they add their words to the string buffer simultaneously. This results in nonsense being printed out for the sentences, showing that synchronization in our next example is useful in avoiding this issue.
package ThreadingExample;
public class ThreadingExample {
private static final StringBuffer STRING_BUFFER = new StringBuffer();
public static void main(String[] args) {
newThread(new String[]{ "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog" });
newThread(new String[]{ "I", "scream,", "you", "scream,", "we", "all", "scream", "for", "ice", "cream" });
}
private static void newThread(String[] wordArray) {
new Thread(new Runnable() {
@Override
public void run() {
try {
slowStringBufffer(wordArray);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}).start();
}
/**
* A perfect example of needing to synchronize code is when a shared resource or object is
* being used. Here we will use a shared StringBuffer object. Without synchronization the
* words get added to the shared StringBuffer at the same time and are all mixed by the time we
* print it out. If we simply synchronize this method then the sentences are built one at a
* time.
*
* @param wordArray
*/
private static void slowStringBuffer(String[] wordArray) throws InterruptedException {
// reset the length
STRING_BUFFER.setLength(0);
// add each word to the string buffer. we will use a small delay to simulate the need
// for synchronization
for (String word : wordArray) {
if (0 != STRING_BUFFER.length()) STRING_BUFFER.append(" ");
STRING_BUFFER.append(word);
Thread.sleep(10);
}
// finally we add a period and print out our sentence
STRING_BUFFER.append(".");
System.out.println(STRING_BUFFER.toString());
}
}
I put the built jar file of this example application into the JNIOR’s flash folder and ran it from the Web UI’s console tab. As you can see it, the threads compete against each other to build their sentences, resulting in nonsense being printed out.
In this second example, the only change that is made is that the slowStringBuffer function is made synchronized. This results in one thread taking control first, making sure it prints out its sentence completely before the next thread can start its sentence. This results in two complete sentences being generated.
package SynchronizedThreadingExample;
public class SynchronizedThreadingExample {
private static final StringBuffer STRING_BUFFER = new StringBuffer();
public static void main(String[] args) {
newThread(new String[]{ "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog" });
newThread(new String[]{ "I", "scream,", "you", "scream,", "we", "all", "scream", "for", "ice", "cream" });
}
private static void newThread(String[] wordArray) {
new Thread(new Runnable() {
@Override
public void run() {
try {
slowStringBuffer(wordArray);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}).start();
}
/**
* A perfect example of needing to synchronize code is when a shared resource or object is
* being used. Here we will use a shared StringBuffer object. Without synchronization the
* words get added to the shared StringBuffer at the same time and are all mixed by the time we
* print it out. If we simply synchronize this method then the sentences are built one at a
* time.
*
* @param wordArray
*/
private static synchronized void slowStringBuffer(String[] wordArray) throws InterruptedException {
// reset the length
STRING_BUFFER.setLength(0);
// add each word to the string buffer. we will use a small delay to simulate the need
// for synchronization
for (String word : wordArray) {
if (0 != STRING_BUFFER.length()) STRING_BUFFER.append(" ");
STRING_BUFFER.append(word);
Thread.sleep(10);
}
// finally we add a period and print out our sentence
STRING_BUFFER.append(".");
System.out.println(STRING_BUFFER.toString());
}
}
I put the built jar file of this example application into the JNIOR’s flash folder and ran it from the Web UI’s console tab. In this example two complete sentences are formed and printed.
Real-Time Mon - Fri, 8am - 4pm EST P: 724-933-9350 PureChat |
Always Available Contact Form sales@integpg.com support@integpg.com |
![]() ![]() |