请教 Thread中的 synchronized(this)的含义 最好请给出个案例 谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 synchronized 用来做线程同步的关键字. 用他修饰的代码或者方法,在同一时刻只允许一个线程操作.具体查一下百度把.很多案例的 public class SynchronizedTest { public static void main(String args[]){ Thread5 t1 = new Thread5(); Thread5 t2 = new Thread5(); t1.start(); t2.start(); }}class Thread5 extends Thread{ static int i = 0; static Object obj = new Object(); public void run(){ while(i<100){ synchronized(obj){ i++; for(int j=0;j<100000;j++){} System.out.println(getName()+": "+i); } } }}public class SynchronizedTest { public static void main(String args[]){ Thread5 t1 = new Thread5(); Thread5 t2 = new Thread5(); t1.start(); t2.start(); }}class Thread5 extends Thread{ static int i = 0; static Object obj = new Object(); public void run(){ while(i<100){ i++; for(int j=0;j<100000;j++){} System.out.println(getName()+": "+i); } }}你自己看一下两个输出的区别吧,慢慢就能理解的了,去网上找些资料看看参考 JAVA桌面程序生成jar包问题在线等啊。。。急急急 求教 怎么在Eclipse中调用oracle的删除的存储过程 RamdonAccessFile乱码问题 有2个简单问题,请大家帮忙~~~ 请教一个问题?为什么在MyEclipse里不能运行? 让最大化变成回复按钮的问题 异常抛出的问题 求各种弹出窗口的语句 怎么让JComboBox对键盘没有响应,只对鼠标有响应啊? 大家给点意见 JAVA泛型求教 JAVA 输入输出流 缓冲区问题
public static void main(String args[]){
Thread5 t1 = new Thread5();
Thread5 t2 = new Thread5();
t1.start();
t2.start();
}
}class Thread5 extends Thread{
static int i = 0;
static Object obj = new Object();
public void run(){
while(i<100){
synchronized(obj){
i++;
for(int j=0;j<100000;j++){}
System.out.println(getName()+": "+i);
}
}
}
}
public class SynchronizedTest {
public static void main(String args[]){
Thread5 t1 = new Thread5();
Thread5 t2 = new Thread5();
t1.start();
t2.start();
}
}class Thread5 extends Thread{
static int i = 0;
static Object obj = new Object();
public void run(){
while(i<100){
i++;
for(int j=0;j<100000;j++){}
System.out.println(getName()+": "+i);
}
}
}
你自己看一下两个输出的区别吧,慢慢就能理解的了,去网上找些资料看看参考