如何在Java中用线程创建一个Applet

238 阅读4分钟

在Java中用线程创建一个小程序

线程通过允许程序同时执行许多任务而使其更有效地运行。它们被用在后台执行复杂的任务,而不干扰主程序。

线程改善了使用浏览器时执行的操作。

前提条件

要继续学习本教程,你应该。

  • 具备[线程]的基本知识。
  • 具备[Java]编程的基本技能。
  • 理解[小程序]。

创建一个线程类

Java有几个基本工具。线程类是用于开发多线程系统的一种。线程类用于创建一个子类,其行为与线程本身一样。该子类是在建立一个函数线程时声明的。

在开发完子类后,线程run() 方法被重载,导致它使线程类表现得像一个线程,定义了类的执行线程。start() 函数被用来调用run() 方法,一个类被实例化了。

// We use this format to create a thread class
public class ThreadClass extends Thread{
    public void run(){
        super.run();
        //Type your code here
    }
}

public class Main{
    public static void main(String args[]){
        ThreadClass thread = new ThreadClass();
        thread.start();//calls the ThreadClass created
    }
}

用线程创建一个小程序

要设计一个使用线程的小程序,我们必须做一些改变。

  • 创建一个类定义。
  • 更新你的applet类的签名,添加实现Runnable 的短语。
  • 知道在线程小程序中要持有的实例变量。
  • 改变你的start() 方法,使其只做生成一个线程并启动它。
  • 写一个run() 方法,其中包含启动你的小程序的实际代码。

要创建一个带有线程的小程序,你必须执行几个动作。我们需要在类定义中定义第一行,如下图所示。

//Defines the class of the applet as public
public class AppletClass extends java.applet.Applet
{
    ...
}

你需要改变你的小程序类的签名,如图所示。

public class AppletClass extends java.applet.Applet implements Runnable
{
    ...
}

上面的程序类支持runnable接口。请记住,接口是一种机制,可以添加许多类所共享的方法名称。然后,它们可以在需要实现behavior 的不同类中被组合和实现。

run() 方法是由可运行接口识别的,然后我们添加一个start() 函数。比如说。

// Function called when the thread execution starts.
public start()
 {
 if (runner == null);
 {
  runner = new Thread(this);
  runner.start();
 }
 }

一旦用户修改了start() 方法,它除了重新生成一个线程外,什么也不做。小程序的主体被切换到一个新的方法,run()

public static void run()
{
 // what your applet does
}

run() 方法包含了你可能希望在一个单独的线程中运行的东西。比如初始化代码,你的小程序的前一个循环,或者其他在其线程中运行的东西。这允许你在run() 内创建新的对象和调用方法,同时也允许对象在线程内传递。

在运行线程后,线程的执行会被停止。这只能通过包括stop() 方法来实现,如下图所示。

public void stop()
{
if (runner != null)
{
runner.stop();//stops execution
runner = null;//sets variable to value nulll
}
}

前面代码中的stop() 方法实现了两件事。

  1. 禁用线程的执行。
  2. 将线程的变量(runner)设置为null--当一个变量被设置为null时,线程对象会使用其之前包含的变量进行垃圾回收。这导致小程序在预定的时间后被从内存中清除掉。start方法设置一个新的线程并重新加载页面。结果是,我们创建了一个在这个小程序上运行的线程。

用线程创建一个小程序的程序

// Applet window or skeleton is developed.
import java.applet.*;
import java.awt.*;
public class Thread extends Applet() {
    float h, w;
    boolean thread;
    Thread t = null;
public void init(){
    //initialization takes place
    system.out.println("init(): start");
    w = getSize().w;// Defines the width
    h = getSize().h;//defines the Height
    setBackground(color.gray);
    system.out.println("init(): stop");
}
// Function called when the applet is terminated or stopped to excute.
public void destroy(){
    //performs shutdown action to the thread.
    system.out.println("destroy()");
}
 // Function called when the applet is started.
public void start() {
    system.out.println("start(): start");
    if ( t == null){
        system.out.println("start(): created thread");
       t = new Thread();
       system.out.println("start(): starting thread");
       t = false;
       t.start();
    }
    else{
        if(thread){
            thread = true;
            system.out.println("start(): notifies thread");// Sends information to the thread
            synchronized(this){
                show();
            }
        }
    }
    system.out.println("start(): stop");
}
}

创建一个HTML文件来执行你的小程序代码

为了执行一个小程序,人们需要创建一个HTML文件,在命令行界面(CLI)中执行该程序。

<html>
  <head>
    <title>MyApplet</title>
    <!--Write the name of the file you created here at MyApplet  -->
  </head>
  <body>
    <applet code="MyApplet.class" width="1024" height="750"></applet>
  </body>
</html>

写完代码后,我们编译小程序,就会加载我们创建的小程序的显示。

总结

在这篇文章中,我们学习了如何在Java中使用线程来创建小程序。我们研究了使用线程创建一个小程序所需的各种变化。我们还学习了如何使用HTML文件执行一个小程序。