Java Special Development Package, jxab,xml, concurrency

Source: Internet
Author: User
1 Sync lock and unlock
2 Atomic type data
3 thread pool

1 Jxab Examples




Package com.newegg.ec.kafka.util;


Import Java.io.File;
Import Java.util.HashSet;
Import Java.util.Set;


Import Javax.xml.bind.JAXBContext;
Import javax.xml.bind.JAXBException;
Import Javax.xml.bind.Marshaller;
Import Javax.xml.bind.Unmarshaller;
Import Javax.xml.bind.annotation.XmlAttribute;
Import javax.xml.bind.annotation.XmlElement;
Import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
Class Customer {
String name;
int age;
int id;
Set<book> Bookset;
@XmlElement
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}

@XmlElement
public int getage () {
return age;
}
public void Setage (int age) {
This.age = age;
}

@XmlAttribute
public int getId () {
return ID;
}
Public set<book> Getbookset () {
return bookset;
}
public void Setbook (set<book> bookset) {
This.bookset = Bookset;
}
public void setId (int id) {
This.id = ID;
}

@Override
Public String toString () {
Return "Customer [id=" + ID + ", name=" + name + ", age=" + Age + "]";
}
}





Marshaller
public class Testjxab {
public static void Main (string[] args) {

Customer customer = new Customer ();
Customer.setid (100);
Customer.setname ("suo");
Customer.setage (29);

Book book = new book ();
Book.setid ("1");
Book.setname ("Harry Potter");

Book Book2 = new book ();
Book2.setid ("2");
Book2.setname ("Apple");

set<book> bookset = new hashset<book> ();
Bookset.add (book);
Bookset.add (BOOK2);

Customer.setbook (Bookset);

try {
File File = new file ("Src/main/resources/configjxab.xml");
Jaxbcontext Jaxbcontext = jaxbcontext.newinstance (Customer.class);
Marshaller Jaxbmarshaller = Jaxbcontext.createmarshaller ();
Output Pretty Printed
Jaxbmarshaller.setproperty (Marshaller.jaxb_formatted_output, true);
Jaxbmarshaller.marshal (customer, file);
Jaxbmarshaller.marshal (Customer, System.out);
catch (Jaxbexception e) {
E.printstacktrace ();
}

Read ();

}
static void Read () {
try {
File File = new file ("Src/main/resources/configjxab.xml");
Jaxbcontext Jaxbcontext = jaxbcontext.newinstance (Customer.class);
Unmarshaller Jaxbunmarshaller = Jaxbcontext.createunmarshaller ();
Customer customer = (customer) jaxbunmarshaller.unmarshal (file);
SYSTEM.OUT.PRINTLN (customer);
catch (Jaxbexception e) {
E.printstacktrace ();
}
}
}




<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?>
<customer id= ">"
<age>29</age>
<name>suo</name>
</customer>




2 Java Concurrent Instances




Atomicinteger Introduction
This class is really practical, and more importantly, it's really very simple:


Attach your own code, you can try it yourself:


Atomicinteger, a class that provides an integer for atomic operations. In the Java language, ++i and i++ operations are not thread-safe and will inevitably use the Synchronized keyword when used. and Atomicinteger through a thread-safe addition and subtraction operation interface.





Code:


Package test;


Import Java.util.concurrent.atomic.AtomicInteger;
/**
* Look at the interface provided by Atomicinteger.


Get the current value

Public final int Get ()

Take the current value and set the new value

Public final int Getandset (int newvalue)

Gets the current value and increases itself

Public final int getandincrement ()

Get the current value and subtract

Public final int getanddecrement ()

Gets the current value and adds the expected value

Public final int getandadd (int delta)




* @author Yangbaobao
*
*/
public class Atomicintegerdemo {
public static void Main (string[] args) {
Atomicinteger ai=new atomicinteger (0);
int I1=ai.get ();
V (i1);
int I2=ai.getandset (5);
V (i2);
int I3=ai.get ();
V (i3);
int i4=ai.getandincrement ();
V (I4);
V (Ai.get ());

}
static void V (int i)
{
System.out.println ("I:" +i);
}
}




3 XML Read instances




Package com.newegg.ec.kafka.util;


Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.InputStream;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;


Import Javax.xml.stream.XMLEventReader;
Import Javax.xml.stream.XMLInputFactory;
Import javax.xml.stream.XMLStreamException;
Import Javax.xml.stream.events.Attribute;
Import javax.xml.stream.events.EndElement;
Import javax.xml.stream.events.StartElement;
Import javax.xml.stream.events.XMLEvent;


Class Item {
Private String date;
private String mode;
Private String Unit;
Private String current;
Private String Interactive;

Public String getDate () {
return date;
}

public void setdate (String date) {
This.date = date;
}
Public String GetMode () {
return mode;
}
public void SetMode (String mode) {
This.mode = mode;
}
Public String Getunit () {
return to Unit;
}
public void Setunit (String unit) {
This.unit = unit;
}
Public String GetCurrent () {
return to current;
}
public void Setcurrent (String current) {
This.current = current;
}
Public String getinteractive () {
return interactive;
}
public void Setinteractive (String interactive) {
this.interactive = interactive;
}


@Override
Public String toString () {
Return "Item [current=] + current +", date= "+ Date +", interactive= "
+ Interactive + ", mode=" + mode + ", unit=" + unit + "];
}
}












Class Staxparser {
Static final String date = "Date";
Static final String item = "Item";
Static final String mode = "mode";
Static final String unit = ' unit ';
Static final String current = ' current ';
Static final String INTERACTIVE = "INTERACTIVE";


@SuppressWarnings ({"Unchecked", "null"})
Public list<item> readconfig (String configfile) {
list<item> items = new arraylist<item> ();
try {
Create a new Xmlinputfactory
Xmlinputfactory inputfactory = Xmlinputfactory.newinstance ();
Setup a new Eventreader
InputStream in = new FileInputStream (configfile);
Xmleventreader Eventreader = Inputfactory.createxmleventreader (in);
Read the XML document
Item item = NULL;


while (Eventreader.hasnext ()) {
Xmlevent event = eventreader.nextevent ();


if (Event.isstartelement ()) {
Startelement startelement = Event.asstartelement ();
If we have an item element, we create a new item
if (Startelement.getname () getlocalpart () = = (ITEM)) {
item = new item ();
We read the attributes from this tag and add the date
attribute to our Object
iterator<attribute> attributes = Startelement
. GetAttributes ();
while (Attributes.hasnext ()) {
attribute = Attributes.next ();
if (Attribute.getname (). toString (). Equals (DATE)) {
Item.setdate (Attribute.getvalue ());
}
}
}


if (Event.isstartelement ()) {
if (Event.asstartelement (). GetName (). Getlocalpart ()
. Equals (MODE)) {
event = Eventreader.nextevent ();
Item.setmode (Event.ascharacters (). GetData ());
Continue
}
}
if (Event.asstartelement (). GetName (). Getlocalpart ()
. Equals (unit)) {
event = Eventreader.nextevent ();
Item.setunit (Event.ascharacters (). GetData ());
Continue
}


if (Event.asstartelement (). GetName (). Getlocalpart ()
. Equals (current)) {
event = Eventreader.nextevent ();
Item.setcurrent (Event.ascharacters (). GetData ());
Continue
}


if (Event.asstartelement (). GetName (). Getlocalpart ()
. Equals (INTERACTIVE)) {
event = Eventreader.nextevent ();
Item.setinteractive (Event.ascharacters (). GetData ());
Continue
}
}
If we reach the ' end of ' an item element, we add it to the list
if (Event.isendelement ()) {
EndElement endelement = Event.asendelement ();
if (Endelement.getname () getlocalpart () = = (ITEM)) {
Items.Add (item);
}
}


}
catch (FileNotFoundException e) {
E.printstacktrace ();
catch (Xmlstreamexception e) {
E.printstacktrace ();
}
return items;
}


}








public class Testxml {
public static void Main (String args[]) {
Staxparser read = new Staxparser ();
list<item> readconfig = Read.readconfig ("Src/main/resources/configxml.xml");
for (Item item:readconfig) {
SYSTEM.OUT.PRINTLN (item);
}
}
}


<?xml version= "1.0" encoding= "UTF-8"?>
<config>
<item date= "January 2009" >
<mode>1</mode>
<unit>900</unit>
<current>1</current>
<interactive>1</interactive>
</item>
<item date= "February 2009" >
<mode>2</mode>
<unit>400</unit>
<current>2</current>
<interactive>5</interactive>
</item>
<item date= "December 2009" >
<mode>9</mode>
<unit>5</unit>
<current>100</current>
<interactive>3</interactive>
</item>
</config>


Learn Jxab Web site


Http://www.vogella.com/tutorials/JAXB/article.html




Java Special Development Package, jxab,xml, concurrency
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.