JAVA练习8 多线程

掌握:java中实现多线程的两种方法,能编写多线程的简单程序。多线程的相关知识还可与管道流结合、或应用他们编写小程序界面的简单动画等(参考练习15)。

1.请编写一个类,类名为subThread ,它是Thread 类的子类。该类中定义了含一个字符串参数的构造函数和run( )方法,方法先在命令行显示线程的名称,然后随机休眠小于1秒的时间,最后显示线程结束信息: "finished"+线程名。编写Application,在其中创建subThread类的三个对象t1、t2、t3,它们的名称分别为"First"、" Second"、" Third",并启动这三个线程。

public  class  Class1

{

public  static  void  main( String[ ]  args )

{

Thread  t1 = new  subThread( "First" );

Thread  t2 = new  subThread( "Second" );

Thread  t3 = new  subThread( "Third" );

t1.start( );t2.start( );t3.start( );    

    }

}

class  subThread  extends  Thread

{ public  subThread( String  str )

{ super( str );  }

   public  void  run( )                

   {       

System.out.println( " " + getName( ) );

try

{ sleep( ( int )( Math.random( ) * 1000 ) ); }

catch( InterruptedException e )

{ }

System.out.println( "Finished!" + getName( ) );

}

}

2.请编写一个类,类名为subThread ,它是Thread 类的子类。该类中定义了含一个字符串参数的构造函数和run( )方法,方法中有一个for循环,循环一共进行5次,循环体中先在命令行显示该线程循环到了第几次,然后随机休眠小于1秒的时间,循环结束后显示线程结束信息: 线程名+"finished"。编写Application,在其中创建subThread类的三个对象t1、t2、t3,它们的名称分别为"First"、" Second"、" Third",并启动这三个线程。

public  class  Class1

{ public  static  void  main( String[ ]  args )

{

Thread  t1 = new  subThread( "First" );

Thread  t2 = new  subThread( "Second" );

Thread  t3 = new  subThread( "Third" );

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++)

       {

        System.out.println( getName( )+", "+i+" times" );

try

{ sleep( ( int )( Math.random( ) * 1000 ) );  }

              catch( InterruptedException e )

{ }

}        

System.out.println( "Finished!" + getName( ) );

}

}

3.请编写一个类,类名为MulThread ,类中定义了含一个字符串参数的构造函数,并实现了Runnable接口,接口中的run( )方法如下实现:方法中先在命令行显示该线程信息,然后随机休眠小于1秒的时间,最后后显示线程结束信息: "finished"+线程名。编写Application,在其中通过Runnable创建MulThread类的三个线程对象t1、t2、t3,并启动这三个线程。

public  class  Class1

{

public  static  void  main( String[ ]  args )

{

Runnable r1 =new  MulThread( "First" );

Runnable r2 =new  MulThread( "Second" );

Runnable r3 =new  MulThread( "Third" );

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( "Finished!  " + s );         

}

}

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()

{

this.setBackground(Color.black);

wordFont=new Font("楷体_gb2312",Font.BOLD,50);

}

public void start()

{

if(timeThread==null)

{

timeThread=new Thread(this);

timeThread.start();

}

}

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+"年"+month+"月"+day+"日";

String s2="星期"+weekday;

String s3=hour+":"+minute+":"+second;

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",Font.BOLD,35));

g.drawString( String.valueOf(counter),60,60 );

}

}

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等天晴i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值