Java thread relay demo

zhaozj2021-02-16  61

Package applet; import java.applet. *; import java.awt. *; import java.awt.event.ActionEvent;

Public Class ThreadTest Extends Applet Implements Runnable {? graphics mypen = null ;? thread athlete1, athlete2 ;? flowLayout flowLayout1 = new flowLayout () ;? int x = 10, y = 10;

PUBLIC VOID INIT ()? {????? mypen = getgraphics (); ??? Athlete1 = new thread (this); ??? Athlete2 = new three (this) ;?}? public void start ()? {? Athlete1.start (); ?? athlete2.start () ;?}

Public void Run () {??? / ** @ Todo Implement this java.lang.runnable method * /

???? f (thread.currentthread () == athlete1) {?????? while (TRUE) ?????? {???????? x = x 1;??? ???? mypen.setcolor (color.blue); ??????? mypen.fillRect (x, 85, 5, 5); ?????? ife (x> = 200) {??? ??????? athlete1.interrupt (); ????????} ?????? try {?????? athlete1.sleep (10); ????} ?? ?? Catch (InterruptedException E) {?????? Break; ????} ????} ????} ???? f (thread.currentthread () == athlete2) {??? ??? while (TRUE) ?????? {????? y = y 1; ???? mypen.setcolor (color.red); ???? mypen.fillRect (Y, 95, 5, 5); ??? if (y> = 250) {??????? athlete2.interrupt (); ?????} ??? Try {??? athlete2.sleep (10); ?}? catch (interruptexception e) {??? Break;?}} ???}?}}

转载请注明原文地址:https://www.9cbs.com/read-21016.html

New Post(0)