62,620
社区成员
发帖
与我相关
我的任务
分享
ublic class ThreadInterview implements Runnable{
int b = 10;
public int getB(){
return b;
}
public synchronized void m1() throws Exception{
b = 3000;
//try{
Thread.sleep(2000);
//}catch(Exception e){
//e.printStackTrace();
//}
System.out.println(Thread.currentThread().getName()+" b=" + b);
}
//public synchronized void m2() throws Exception{
public void m2() throws Exception{
//b = 2000;
//try{
Thread.sleep(1000);
b = 2000;
//}catch(Exception e){
//e.printStackTrace();
//}
}
public void run(){
try{
m1();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ThreadInterview TI = new ThreadInterview();
Thread t = new Thread(TI);
t.start();
TI.m2();
System.out.println(Thread.currentThread().getName() + " b=" +TI.b);
}
}
package cn.fee.arithmetic;
public class TestDeadLock implements Runnable {
public int t;
static Object o1 = new Object();
static Object o2 = new Object();
public static void main(String[] args) {
TestDeadLock d1 = new TestDeadLock();
TestDeadLock d2 = new TestDeadLock();
d1.t = 0;
d2.t = 1;
Thread t1 = new Thread(d1);
Thread t2 = new Thread(d2);
t1.start();
t2.start();
}
public void run() {
System.out.println(t);
if (t == 1) {
synchronized (o1) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o2) {
System.out.println("1");
}
}
}
if (t == 0) {
synchronized (o2) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o1) {
System.out.println("0");
}
}
}
}
}