JAVA多线程的实现方式包括三种,但追根究底其实是一种,都是通过Thread类来实现的。具体代码表现为三种:直接继承Thread类,实现Runnable接口,实现Callable接口。
1.创建一个Thread的子类,重写run()方法,使用的时候创建一个Thread的对象,调用start()方法,该方法继承Thread类,特点是代码实现比较简单,该子类应该就是当作线程类来使用,应该有具体的线程逻辑。
2.自定义的类通过实现Runnable接口,重写run()方法,使用的时候创建普通类的对象,再创建Thread类的对象把普通类对象装进Thread类里面,通过Thread类对象调用start()方法启动线程。通过接口实现的特点是还可以继承其他类,比较灵活,而且说明这个类本身并不是作为线程类来使用的。
3.自定义的类通过实现Callable接口,重写call()方法,使用的时候常见普通类对象,再创建FutureTask对象,把普通类对象装进FutureTask里面,再创建Thread类对象,把FutureTask对象装进Thread类,通过Thread类对象调用start()方法启动线程,通过FutureTask的对象调用get方法获取返回值。Callable接口和Runnable的区别在于call()方法具有返回值,在和Runnable使用场景类似但又需要使用返回值的时候就可以使用Callable接口
在多线程的情况下,必须共享资源的时候,数据被多个线程同时访问和修改的话数据就会出错,为了防止数据被同时访问的时候出现错乱的情况,需要加同步锁以实现线程安全。这时候要使用synchonized关键字,使用方法有两种:
一种是直接在方法上面加synchonized关键字,在访问修饰符和返回值之间。另一种是在方法体内用synchronized(this/****.class){代码块}来实现
xml的使用无非是基本语法以及增删改查。
先讲一下基本语法:有5个需要使用通用字符转的字符【<】【>】【"】【&】【'】分别对应【<】【>】【"】【&】【'】,但是严格来说,在XML中只有”<”和”&”是非法的,其它三个都是可以合法存在的,但容易引起错误,因此这五个都转义比较好。单个字符转义就直接使用通用字符,整段文字都需要转义的时候可以用另一种方式让整篇不进行编译。方法是用<![CDATA[开始*****内<'>&"容*****结束]]>
这个标记所包含的内容将表示为纯文本。
<?xml version="1.0" encoding="GB2312" ?>
<!--xml的行首信息,没有并不会报错,但解析可能出错--><!--这是xml的注释-->
<score><!--根节点,没有一定会报错-->
<student1 name="王显明">
<name>王显明</name>
<预计得分>75</预计得分>
<实际得分>80</实际得分>
</student1><!--节点必须成对-->
<student2 name="宋佳">
<name>宋佳</name>
<预计得分>75</预计得分>
<实际得分>88</实际得分>
</student2><!--节点必须成对-->
</score><!--根节点,节点必须成对-->
下面是xml一段代码实例:
public class XmlSave {
Document document=null;
public void getDoc(String path) throws Exception {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document= builder.parse(path);
}
public void showDocument(){
NodeList nl = document.getElementsByTagName("book");
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
Element ele=(Element)node;
System.out.println("id="+ele.getAttribute("id")+",name="+ele.getAttribute("name"));
NodeList childNodes = ele.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node childnode = childNodes.item(j);
if (childnode.getNodeType()==Node.ELEMENT_NODE){
Element ec=(Element)childnode;
Text t1=(Text)childnode.getFirstChild();
System.out.println(ec.getTagName()+"="+t1.getWholeText());
}
}
}
}
public void change(){
NodeList books = document.getElementsByTagName("book");
Node root = books.item(0).getParentNode();
Element eAdd = document.createElement("book");
eAdd.setAttribute("id","141");
eAdd.setAttribute("name","History");
Element author = document.createElement("author");
Text t1 = document.createTextNode("司马迁");
author.appendChild(t1);
Element price = document.createElement("price");
Text t2 = document.createTextNode("30");
price.appendChild(t2);
Element title = document.createElement("title");
Text t3 = document.createTextNode("史记");
title.appendChild(t3);
eAdd.appendChild(author);
eAdd.appendChild(price);
eAdd.appendChild(title);
root.appendChild(eAdd);
}
public void saveXML(String path) throws Exception {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(path));
transformer.transform(source,result);
}
public static void main(String[] args) throws Exception {
XmlSave books=new XmlSave();
books.getDoc("src\\kb15\\xml\\sample.xml");
books.change();
books.saveXML("src\\kb15\\xml\\new.xml");
}
}