教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 实用模板 >

Java程序上机练习题(8)

来源:网络收集 时间:2026-08-03
导读: catch( InterruptedException e ) { } System.out.println( \ } } 2.请编写一个类,类名为subThread ,它是Thread 类的子类。该类中定义了含一个字符串参数的构造函数和run( )方法,方法中有一个for循环,循环一共

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字,全部文档内容请下载后查看。喜欢就下载吧 ……
Java程序上机练习题(8).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/520002.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)