23,405
社区成员
发帖
与我相关
我的任务
分享
class TestThread extends Thread
{
public static int count=1 ;
public static Object obj = new Object();
public TestThread(String name){
super(name);
}
public void run(){
synchronized(obj){
for(int i=0;i<10;i++){
System.out.println(currentThread().getName()+"说:我数"+count++);
obj.notifyAll();
try{
obj.wait();
}catch(Exception e){
System.out.println("错误了");
}
}
obj.notifyAll();
}
}
public static void main(String[] args)
{
//两个小孩
for(int i= 1;i<3;i++){
new TestThread("小孩"+i).start();
}
}
}
class TestThread extends Thread
{
public static int count ;
public TestThread(String name){
super(name);
}
public void run(){
//线程循环十次
for(int i = 0;i<10;i++){
//加锁同步
synchronized(TestThread.class){
System.out.println(currentThread().getName()+"||count="+count++);
}
}
}
public static void main(String[] args)
{
//开启十个线程
for(int i= 0;i<10;i++){
new TestThread("线程"+i).start();
}
}
}
class TestThread extends Thread
{
public static int count;
public TestThread(String name){
super(name);
}
public void run(){
//每个线程循环十次
for(int i=0;i<10;i++){
System.out.println(currentThread().getName()+"|| count="+count++);
}
}
public static void main(String[] args)
{
//十个线程
for(int i = 0;i<10;i++){
new TestThread("线程"+i).start();
}
}
}