public class ThreadTest2 {
public static void main(String[] args){
System.out.println("begin our test");
ThreadSleep sleep = new ThreadSleep();
try {
Thread thread1 = new Thread(sleep,"路人甲");
Thread thread2 = new Thread(sleep,"路人乙");
thread1.start();
thread2.start();
}catch(Exception e){
e.printStackTrace();
}
System.out.println("test is over");
}
}
class ThreadSleep implements Runnable{
int count = 0;
@Override
public void run(){
System.out.println(Thread.currentThread().getName() + " say : hello sleep !!");
count();
}
public void count(){
while(count < 20) {
System.out.println(Thread.currentThread().getName() + " say : count is " + count);
try {
count++;
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
begin our test test is over 路人甲 say : hello sleep !! 路人甲 say : count is 0 路人乙 say : hello sleep !! 路人乙 say : count is 1 路人甲 say : count is 2 路人乙 say : count is 2 路人甲 say : count is 4 路人乙 say : count is 4 路人甲 say : count is 6 路人乙 say : count is 7 路人乙 say : count is 8 路人甲 say : count is 8 路人甲 say : count is 10 路人乙 say : count is 10 路人乙 say : count is 12 路人甲 say : count is 12 路人乙 say : count is 14 路人甲 say : count is 14 路人甲 say : count is 16 路人乙 say : count is 16 路人甲 say : count is 18 路人乙 say : count is 18
public class ThreadTest2 {
public static void main(String[] args){
System.out.println("begin our test");
ThreadSleep sleep = new ThreadSleep();
try {
Thread thread1 = new Thread(sleep,"路人甲");
Thread thread2 = new Thread(sleep,"路人乙");
thread1.start();
thread2.start();
}catch(Exception e){
e.printStackTrace();
}
System.out.println("test is over");
}
}
class ThreadSleep implements Runnable{
int count = 0;
@Override
public void run(){
System.out.println(Thread.currentThread().getName() + " say : hello sleep !!");
count();
}
public void count(){
while(count < 20) {
synchronized (this) {
System.out.println(Thread.currentThread().getName() + " say : count is " + count);
try {
count++;
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
begin our test 路人甲 say : hello sleep !! 路人甲 say : count is 0 test is over 路人乙 say : hello sleep !! 路人甲 say : count is 1 路人甲 say : count is 2 路人甲 say : count is 3 路人甲 say : count is 4 路人甲 say : count is 5 路人甲 say : count is 6 路人甲 say : count is 7 路人甲 say : count is 8 路人甲 say : count is 9 路人甲 say : count is 10 路人甲 say : count is 11 路人甲 say : count is 12 路人甲 say : count is 13 路人甲 say : count is 14 路人甲 say : count is 15 路人甲 say : count is 16 路人甲 say : count is 17 路人甲 say : count is 18 路人甲 say : count is 19 路人乙 say : count is 20
public class ThreadTest2 {
public static void main(String[] args) {
System.out.println("begin our test");
ThreadSleep sleep = new ThreadSleep();
try {
Thread thread1 = new Thread(sleep, "路人甲");
Thread thread2 = new Thread(sleep, "路人乙");
thread1.start();
thread2.start();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("test is over");
}
}
class ThreadSleep implements Runnable {
int count = 0;
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " say : hello sleep !!");
count();
}
public void count() {
while (count < 20) {
synchronized (this) {
System.out.println(Thread.currentThread().getName() + " say : count is " + count);
try {
count++;
this.wait(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
begin our test 路人甲 say : hello sleep !! 路人甲 say : count is 0 test is over 路人乙 say : hello sleep !! 路人乙 say : count is 1 路人甲 say : count is 2 路人乙 say : count is 3 路人甲 say : count is 4 路人乙 say : count is 5 路人甲 say : count is 6 路人乙 say : count is 7 路人甲 say : count is 8 路人乙 say : count is 9 路人甲 say : count is 10 路人乙 say : count is 11 路人甲 say : count is 12 路人乙 say : count is 13 路人乙 say : count is 14 路人甲 say : count is 15 路人乙 say : count is 16 路人甲 say : count is 17 路人乙 say : count is 18 路人甲 say : count is 19
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有