标签:adt rri system start run exce pack stack exception
题目:
编写一个创建三个线程对象的程序。每个线程应该输出一则消息,并且消息后紧跟字符串“消息结束”。
在线程输出消息后,应暂停一秒钟,然后才输出“消息结束”,首先应该由线程1输出消息,然后是线程2和线程3。
package multithread; public class ThreadTest { public static void main(String[] args) { showMsg s = new showMsg(); callMsg c1 = new callMsg(s, "消息1"); callMsg c2 = new callMsg(s, "消息2"); callMsg c3 = new callMsg(s, "消息3"); c1.start(); c2.start(); c3.start(); } } class showMsg { public void call(String msg) { System.out.println(msg); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("消息结束"); } } class callMsg extends Thread { showMsg showmsg; String msg; public callMsg(showMsg showmsg, String msg) { this.showmsg = showmsg; this.msg = msg; } @Override public void run() { synchronized (showmsg) { showmsg.call(msg); } } }
标签:adt rri system start run exce pack stack exception
原文地址:http://www.cnblogs.com/tangjiang-code/p/7857976.html