Thread

debut.cis.nctu.edu.tw

Thread

public class Reminder {


// Timer

private Timer timer;

public Reminder(int seconds) {




// Timer

timer = new Timer();

// seconds ,

timer.schedule(new RemindTask(),

seconds*1000);

}




class RemindTask extends TimerTask {

public void run() {


}

}

System.out.println("Time's up!");

timer.cancel();

// Time's up

// timer thread

public static void main(String args[]) {

System.out.println("About to schedule task.");

new Reminder(5);


System.out.println("Task scheduled.");

}

}

// main

// Reminder : 5



//

11:01:00 pm



.getInstance(); //

calendar.set(Calendar.HOUR_OF_DAY, 23);

// => 23

calendar.set(Calendar.MINUTE, 1); // => 1

calendar.set(Calendar.SECOND, 0); // => 0

Date time = calendar.getTime();

// time (Date=> 1/1/1970


milliseconds)

//

timer = new Timer();

timer.schedule(new RemindTask(), time);



class


timer

! ( class )


import java.util.Timer;

import java.util.TimerTask;

class RemindTask extends TimerTask {


Timer timer;

//


RemindTask(Timer timer) {

this.timer=timer;

}

public void run() {

System.out.println("Time's up!");

}

}

//Terminate the timer thread

timer.cancel();

public class MainClass {

private Timer timer;

//

public MainClass (int seconds) {

timer = new Timer();

RemindTask obj=new RemindTask(timer);

timer.schedule(obj, seconds*1000);

}

}

public static void main(String args[]) {

System.out.println("About to schedule task.");

new MainClass(5);

System.out.println("Task scheduled.");

}


Timer obj= new Timer();

obj.scheduleAtFixedRate(TimerTask ,

Date , long period) ;

obj.scheduleAtFixedRate(TimerTask ,

long delay, long period) ;

obj.schedule(TimerTask ,

Date , long period) ;

obj.schedule(TimerTask ,


long delay, long period) ;







class RemindTask extends TimerTask {

// 5

run

public void run() {

System.out.println("Time's up!");

}

}

public class Reminder {

private Timer timer ;

public Reminder(int seconds) {


Timer

timer=new Timer();

// Second run

timer.schedule(new RemindTask (),0, seconds*1000);


}

public static void main(String args[]) {

System.out.println("About to schedule task.");

new Reminder(5);

System.out.println("Task scheduled.");

}

}









// SimpleThread Thread

public class SimpleThread extends Thread {




public SimpleThread(String str) {


// Thread


super(str);


}

// SimpleThread


thread


public void run() {


for (int i = 0; i < 10; i++) {


//

Thread

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

try {



// sleep random

sleep((long)(Math.random() * 1000));


} catch (InterruptedException e) {}


}

// thread


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

}


}

public class TwoThreadsDemo {

public static void main (String[] args) {

//

Thread,


new SimpleThread("Jing").start();


//

Thread,


new SimpleThread("Jenny").start();

}

}


import java.awt.Graphics;

import java.util.*;

import java.text.DateFormat;

import java.applet.Applet;

// for Graphics object

// for Thread

// Clockapplet

Runnable

public class Clock extends Applet implements Runnable {

// Thread

private Thread clockThread = null;



// applet start method

public void start() {

// re-start, thread

if (clockThread == null) {

// Thread : < class> =this,

// =Clock

clockThread = new Thread(this, "Clock");



// thread

clockThread.start();

}

}


// thread run method: run( )


public void run() {

Thread myThread = Thread.currentThread();

//

, clockThread=null

while (clockThread == myThread) {

repaint();

try {

Thread.sleep(1000);

} catch (InterruptedException e){ }

}

}



public void paint(Graphics g) {

//


Calendar cal = Calendar.getInstance();


//

:


Date date = cal.getTime();



// java.text.DateFormat

//


DateFormat dateFormatter = DateFormat.getTimeInstance();


}

// format method time

g.drawString(dateFormatter.format(date), 5, 10);

// overrides Applet's stop method, not Thread's

public void stop() {

//

, stop. clcokThread=null

//

clockThread = null;


}

}


Clock









































class CustomThread extends Thread {

}

public void run()

{

}

// thread run !

public class isaliveDemo {

public static void main(String args[]) {

//

thread

CustomThread obj=new CustomThread();

// thread

obj.start();


// thread

System.out.println(obj.isAlive());


}

}


public class CustomThread extends Thread {

public void run()

{

// thread run !

try{

for(int i=0;i


class Share{

public void job()

{

//

}

}

//

public class Working implements Runnable { //


public Share ShareObj;

public Working(Share ShareObj){

this.ShareObj=ShareObj;

}

//

public void run() {

//

//


job();

}

}


public class Main {

public static void main(String args[]) {

Share ShareObj=new Share();

Working w1=new Working(ShareObj);

Working w2=new Working(ShareObj);

Thread th1=new Thread(w1,"Th1");

Thread th2=new Thread(w2,"Th2");

// thread= Th1

//

thread=Th2

}

}

th1.start();

th2.start();

// Thread




class Share{

//

synchronized public void job()

{

//


for(int i=0;i


synchronized (ShareObj) {


//

ShareObj.job();

}

}

}

//

ShareObj
















public class DataBase {

final int Max=5;

public int data[]=new int[Max];

public int ItemCounter=0;

synchronized public int ReadItem()

{

// 5

//

DataBase

//

try{

if(ItemCounter==0)

{ System.out.println("Empty: Read Item Waiting");

//

,

wait();



}

System.out.println("Read ...");Thread.sleep(2000);

}catch(InterruptedException e) {};


ItemCounter--;

//

,

notify();



System.out.println("Read=> "+data[ItemCounter]);

return data[ItemCounter];

//

}

synchronizedpublic void WriteItem(int Item)

{

try{

if(ItemCounter==Max)

{ System.out.println("Full: Write Item Waiting");

//

wait();



}

System.out.println("Write ...");Thread.sleep(2000);

}catch(InterruptedException e) {}

data[ItemCounter]=Item;


System.out.println("Write=> "+Item);

ItemCounter++;

// Data

// Counter

}

}

//

,

notify();


class Reader extends Thread {

DataBase db;

public Reader(DataBase db)

{

this.db=db;

}

public void run()

{

for(int i=0;i


public void run()

{

for(int i=0;i


public void init()

{

if(th==null)

{

th=new Thread(this,"Move");

AppletWidth=100;

//

img=getImage(getCodeBase(),"animationdemo/button1.gif");

}

}

public void start()

{

th.start();

}

public void paint(Graphics g)

{

g.drawString(getCodeBase().toString(),0,100);

g.drawImage(img,ImgX,ImgY,this);

}

// thread

public void run()

{

while(true)

{

//

for(int x=0;x0;x-=10)

{

ImgX=x;

//

this.repaint();

try{


}

}

}

}

th.sleep(100);

}catch(InterruptedException e) {}

More magazines by this user
Similar magazines