Java程序上机练习题(8)
catch( InterruptedException e ) { }
System.out.println( \ } }
2.请编写一个类,类名为subThread ,它是Thread 类的子类。该类中定义了含一个字符串参数的构造函数和run( )方法,方法中有一个for循环,循环一共进行5次,循环体中先在命令行显示该线程循环到了第几次,然后随机休眠小于1秒的时间,循环结束后显示线程结束信息: 线程名+\。编写Application,在其中创建subThread类的三个对象t1、t2、t3,它们的名称分别为\、\、\,并启动这三个线程。 public class Class1 {
public static void main( String[ ] args ) {
Thread t1 = new subThread( \Thread t2 = new subThread( \Thread t3 = new subThread( \t1.start( ); t2.start( ); t3.start( );
} }
class subThread extends Thread {
public subThread( String str )
{ super( str ); }
public void run( ) { for(int i=1;i<=5;i++) {
try
System.out.println( getName( )+\
{ sleep( ( int )( Math.random( ) * 1000 ) ); } catch( InterruptedException e ) { } }
}
System.out.println( \
}
3.请编写一个类,类名为MulThread ,类中定义了含一个字符串参数的构造函数,并实现了Runnable接口,接口中的run( )方法如下实现:方法中先在命令行显示该线程信息,然后随机休眠小于1秒的时间,最后后显示线程结束信息: \线程名。编写Application,在其中通过Runnable创建MulThread类的三个线程对象t1、t2、t3,并启动这三个线程。 public class Class1 { }
public static void main( String[ ] args ) { }
Runnable r1 =new MulThread( \Runnable r2 =new MulThread( \Runnable r3 =new MulThread( \Thread t1 = new Thread( r1 ); Thread t2 = new Thread( r2 ); Thread t3 = new Thread( r3 ); t1.start( );t2.start( );t3.start( );
class MulThread implements Runnable {
String s;
public MulThread(String str) { s=str;} public void run( ) {
System.out.println(s); try {
Thread.sleep( ( int ) ( Math.random( ) * 1000 ) ); }
catch( InterruptedException e ){ }
System.out.println( \ \
}
}
4.编写小程序实现一个数字时钟。 import java.awt.*; import java.applet.*;
import java.util.Calendar;
public class Applet1 extends Applet implements Runnable {
Thread timeThread; Font wordFont; int year,month,day; int weekday;
int hour,minute,second; public void init() { }
public void start() {
if(timeThread==null) {
timeThread=new Thread(this); timeThread.start();
this.setBackground(Color.black);
wordFont=new Font(\楷体_gb2312\
} }
public void stop() {
if(timeThread!=null) { }
timeThread.stop(); timeThread=null;
}
public void run() {
while(true) { Calendar time=Calendar.getInstance();//创建类的实例
year=time.get(Calendar.YEAR);
month=time.get(Calendar.MONTH);
day=time.get(Calendar.DAY_OF_MONTH ); weekday=time.get(Calendar.DAY_OF_WEEK); hour=time.get(Calendar.HOUR); minute=time.get(Calendar.MINUTE); second=time.get(Calendar.SECOND); repaint ();
}
try
{ Thread.sleep( 300); } catch (InterruptedException e) { }
}
public void paint (Graphics g) { String s1=year+\年\月\日\
String s2=\星期\
String s3=hour+\g.setFont (wordFont); g.setColor (Color.green); g.drawString (s1, 20, 50); g.drawString (s2, 20, 120); g.drawString (s3, 20, 200);
} }
5.编写小程序实现Runnable接口,通过多线程实现在小程序窗口中不断的显示自然数:从1直到100。 import java.awt.*; import java.applet.*;
public class Javaapplet extends Applet implements Runnable {
int counter=0; Thread t;
public void init() { t=new Thread(this); }
t.start();
public void run() { while( counter<=100 ) {
}
counter++; try
{ Thread.sleep(1000); } catch ( InterruptedException e ) { } repaint();
}
public void paint( Graphics g ) {
setBackground(Color.black); g.setColor(Color.green);
g.setFont(new Font(\Times New Roman\g.drawString( String.valueOf(counter),60,60 );
…… 此处隐藏:1184字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [实用模板]第八章:法国“新浪潮”与“左岸派”
- [实用模板]2021年北京上半年临床医学检验技师生物
- [实用模板]SAP GUI 7.10客户端安装配置文档
- [实用模板]2001年临床执业医师资格考试综合笔试试
- [实用模板]36机场工作实用英语词汇总结
- [实用模板](一)社会保险稽核通知书
- [实用模板]安全教育主题班会材料
- [实用模板]濉溪县春季呼吸道传染病防控应急演练方
- [实用模板]长沙房地产市场周报(1.30-2.3)
- [实用模板]六年级数学上册典中点 - 图文
- [实用模板]C程序设计(红皮书)习题官方参考答案
- [实用模板]中国证监会第一届创业板发行审核委员会
- [实用模板]桥梁工程复习题
- [实用模板]2011学而思数学及答案
- [实用模板]初中病句修改专项练习
- [实用模板]监理学习知识1 - 图文
- [实用模板]小机灵杯四年级试题
- [实用模板]国贸专业毕业论文模板
- [实用模板]教育学概论考试练习题-判断题4
- [实用模板]2015届高考英语一轮复习精品资料(译林
- 00Nkmhe_市场营销学工商管理_电子商务_
- 事业单位考试法律常识
- 诚信教育实施方案
- 吉大小天鹅食品安全检测箱方案(高中低
- 房地产销售培训资料
- 高一地理必修1复习提纲
- 新概念英语第二册lesson_1_练习题
- 证券公司内部培训资料
- 小学英语时间介词专项练习
- 新世纪英语专业综合教程(第二版)第1册U
- 【新课标】浙教版最新2018年八年级数学
- 工程建设管理纲要
- 外研版 必修一Module 4 A Social Surve
- Adobe认证考试 AE复习资料
- 基于H.264AVC与AVS标准的帧内预测技术
- 《食品检验机构资质认定管理办法》(质
- ABB变频器培训课件
- (完整版)小学说明文阅读练习题及答案
- 深思洛克(SenseLock) 深思IV,深思4,深
- 弟子规全文带拼音




