Affichage des articles dont le libellé est Java. Afficher tous les articles
Affichage des articles dont le libellé est Java. Afficher tous les articles

Java threads notification

|
A proper way to temporarily pause the execution of a Java thread is to set a variable that the thread checks occasionally. When the thread detects that the variable is set, it calls its wait() method. The paused thread can then be woken up by another thread by calling the notify() method of the latter.

Note: Thread.suspend() and Thread.resume() provide methods have been deprecated because they are very unsafe. With the approach above, the target thread can ensure that it will be paused in an appropriate place.

// Create and start the thread to pause in a main thread
ThreadToPause threadToPause = new ThreadToPause();
threadToPause.start();

while (true) {
// Do work

// tell the thread to wait
synchronized(threadToPause) {
thread.wait = true;
}

// Do work

// Resume the thread
synchronized(thread) {
thread.pleaseWait = false;
thread.notify();
}

// Do work
}



class ThreadToPause extends Thread {
boolean wait = false;

// Implements the run method
public void run() {
while (true) {
// Do work

// Check if should wait
synchronized (this) {
while (wait) {
try {
wait();
} catch (Exception e) {
}
}
}

// Do work
}
}
}

Java pausing and resuming threads, pause and resume a thread in Java, Thread notifications in Java, waking up a thread in Java, thread wait and wake up

Java xml serialization & deserializatrion

|

Java XML Serialization

Java allow you to easily transform various xml input formats into various output formats by using the java.xml.transform package. In this example, we'll see how to parse an xml document into an array of byte.
// use this package
import javax.xml.transform.*;

// transforms xml document to byte array
// o_doc is a org.w3c.Document object
Source source = new DOMSource(o_doc);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Result result = new StreamResult(baos);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(source, result);
byte[] out = baos.toByteArray();

Java XML Deserialization

The reverse transformation allow you to build a Java Document object from a byte array.
// use this package
import javax.xml.transform.*;

// transforms byte array to xml document
// messenger is a byte array
ByteArrayInputStream bais = new ByteArrayInputStream(messenger);
Source source = new StreamSource(bais);
DOMResult result = new DOMResult();
Transformer transformer = factory.newTransformer();
transformer.transform(source,result);
Node out = result.getNode();

Document to byte array, Java Dom to byte array, Java Document serialization, XML to byte array, Byte array to XML, Byte array deserialization, Byte array to Java DOM Object

Copying nodes between XML documents with Java DOM

|
A recurent problem in Java is to copy nodes from one Document to another one ( org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it). This issue could be done easily by using the Document.importNode method.

bais = new ByteArrayInputStream(donnees);

Document o_doc; // original document
Document r_doc; // resulting document

DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(false);

// Creation du gestionnaire de DOM
DocumentBuilder builder = dfactory.newDocumentBuilder();

o_doc = builder.parse(bais);
r_doc = builder.newDocument();

Element root = r_doc.createElement("Root");
root.setAttribute("xmlns", "http://my.domain.com");
Element oneNode = r_doc.createElement("oneNode");
root.appendChild(oneNode);

r_doc.appendChild(root);

CachedXPathAPI cxpa = new CachedXPathAPI();

Node nodeToCopy = cxpa.selectSingleNode(o_doc, "XPathPathToNodeToCopy");
// use importNode to copy into another tree
oneNode.appendChild(r_doc.importNode(nodeToCopy, true));

Copy one node to one parse Tree into another, Copy a node into a new Document, Copy parts of an xml document into another, Nodes copy in Java, Copy XML nodes in Java