public class Main { public static void main(String[] args) { count c = new count(); Thread t = new Thread(c); t.start(); for (int i = 10; i >= 0; i--) { try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } if (i > 0) { System.out.println(Thread.currentThread().getName() + i); } else { System.out.println(Thread.currentThread().getName()+"新年快乐"); } } } } class count implements Runnable { int count = 10; public synchronized void run() { while(count != 0) { try { Thread.sleep(1000); System.out.println(Thread.currentThread().getName()+count); count--; if(count == 0) { System.out.println(Thread.currentThread().getName()+"新年快乐"); } } catch (InterruptedException e) { throw new RuntimeException(e); } } } }
public class Main
{
public static void main(String[] args)
{
count c = new count();
Thread t = new Thread(c);
t.start();
for (int i = 10; i >= 0; i--)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
throw new RuntimeException(e);
}
if (i > 0)
{
System.out.println(Thread.currentThread().getName() + i);
}
else
{
System.out.println(Thread.currentThread().getName()+"新年快乐");
}
}
}
}class count implements Runnable
{
int count = 10;
public synchronized void run()
{
while(count != 0)
{
try
{
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName()+count);
count--;
if(count == 0)
{
System.out.println(Thread.currentThread().getName()+"新年快乐");
}
}
catch (InterruptedException e)
{
throw new RuntimeException(e);
}
}
}
}
JAVA多线程实现10秒倒数播报新年快乐------JAVA
于 2023-05-05 14:40:16 首次发布