package com.hanqi.xc; import java.util.*; public class lianxi extends Thread { public void run() { chengshi(); } public void chengshi() { for(int i=1;i<=10;i++) { Random n=new Random(); int a= n.nextInt(2); if(a==0) { System.out.println(Thread.currentThread().getName()+"济南"); } else { System.out.println(Thread.currentThread().getName()+"青岛"); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
package com.hanqi.xc; public class lianxi2 { public static void main(String[] args) { lianxi a1=new lianxi(); a1.setName("线程1"); a1.start(); lianxi a2=new lianxi(); a2.setName("线程2"); a2.start(); } }
时间: 2024-11-05 04:07:56