Java线程:线程的同步与锁
一、同步问题提出
线程的同步是为了防止多个线程访问一个数据对象时,对数据造成的破坏。
例如:两个线程ThreadA、ThreadB都操作同一个对象Foo对象,并修改Foo对象上的数据。
public
class
Foo {
private
int
x = 100;
public
int
getX() {
return
x;
}
public
int
fix(int
y) {
x = x - y;
return
x;
}
}
public
class
MyRunnable implements
Runnable {
private
Foo foo = new
Foo();
public
static
void
main(String[] args) {
MyRunnable r = new
MyRunnable();
Thread ta = new
Thread(r, "Thread-A"
);
Thread tb = new
Thread(r, "Thread-B"
);
ta.start();
tb.start();
}
public
void
run() {
for
(int
i = 0; i < 3; i++) {
this
.fix(30);
try
{
Thread.sleep(1);
} catch
(InterruptedException e) {
e.printStackTrace();
}
&
相关文档:
create PROCEDURE pagelist
@tablename nvarchar(50),
@fieldname nvarchar(50)='*',
@pagesize int output,--每页显示记录条数
@currentpage int output,--第几页
@orderid nvarchar(50),--主键排序
@sort int,--排序方式,1表示升序,0表示降序排列
......
这是服务窗口类,模拟四个线程异步运行
public class TicketWindow {
public static void main(String args[]) {
Ticket ticket = new Ticket();
Thread w1 = new Thread(ticket, "1号售票窗口");
Thread w2 = new Thread(ticket, "2号售票窗口");
Thread w3 ......
public class P {
public static void main(String[] args){
String pattern="000";
java.text.DecimalFormat df = new java.text.DecimalFormat(pattern);
int i = 10,j=6;
System.out.println("i="+df.format(i)+"\nj="+df.format(j));
}
}
---------------------输出-----------------------
i=010 ......
/**我这只讲 ListArray ,ListedList,HashMap
//ListArray 它是一个实现了List接口的类 ,List继承collection接口
//调用import java.util.ArrayList包,(这里两者任选其一) 完整的java集合存放在java.util包中
//特点:
1>.List是有序的集合
2>.List可以有重复的元素值
3>.使用索引来精确的访问元素值,
4& ......
java 代码实现
public static boolean isLetter(char c) {
int k = 0x80;
return c / k == 0 ? true : false;& ......