主程式代码:

public class MainApp {

public static void main(String[] args) {
try {
MySprite dog = new MySprite("狗狗");
MySprite cat = new MySprite("喵喵");
MySprite pig = new MySprite("猪猪");
System.out.println("--- start sprites");
dog.start();
cat.start();
pig.start();
Thread.sleep(500);
System.out.println("--- suspend dog");
dog.suspend();
System.out.println("--- main thread do something");
Thread.sleep(500);
System.out.println("--- resume dog");
dog.resume();
Thread.sleep(500);
System.out.println("--- end dog");
dog.stop();
System.out.println("--- main thread do something");
Thread.sleep(500);
System.out.println("--- end other sprites");
cat.stop();
pig.stop();
Thread.sleep(100);
System.out.println("--- exit programe.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
线程实现

public class MySprite implements Runnable {

/*
* 线程用变量
*/
private boolean running = false;
private boolean waiting = false;
private Thread thread;
/*
* Business 变量
*/
private String name;
public MySprite(String name) {
this.name = name;
this.thread = new Thread(this);
}
/**
* 启动线程
*/
public void start() {
running = true;
thread.start();
}
/**
* 挂起线程
*/
public void suspend() {
if (waiting) { // 是挂起状态则直接返回
return;
}
synchronized (this) {
this.waiting = true;
}
}
/**
* 恢复线程
*/
public void resume() {
if (!waiting) { // 没有挂起则直接返回
return;
}
synchronized (this) {
this.waiting = false;
this.notifyAll();
}
}
/**
* 停止线程
*/
public void stop() {
if (!running) { // 没有运行则直接返回
return;
}
synchronized (this) {
running = false;
}
}
public void run() {
for(;;) {
try {
// 线程挂起和退出处理
synchronized (this) {
if (!running) {
break;
}
if (waiting) {
this.wait();
}
}

// 应该做的事情
cry();
// 进入等待状态
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void cry() {
System.out.println(name + ":woo!");
}
}

public class MainApp {
public static void main(String[] args) {
try {
MySprite dog = new MySprite("狗狗");
MySprite cat = new MySprite("喵喵");
MySprite pig = new MySprite("猪猪");
System.out.println("--- start sprites");
dog.start();
cat.start();
pig.start();
Thread.sleep(500);
System.out.println("--- suspend dog");
dog.suspend();
System.out.println("--- main thread do something");
Thread.sleep(500);
System.out.println("--- resume dog");
dog.resume();
Thread.sleep(500);
System.out.println("--- end dog");
dog.stop();
System.out.println("--- main thread do something");
Thread.sleep(500);
System.out.println("--- end other sprites");
cat.stop();
pig.stop();
Thread.sleep(100);
System.out.println("--- exit programe.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
线程实现

public class MySprite implements Runnable {
/*
* 线程用变量
*/
private boolean running = false;
private boolean waiting = false;
private Thread thread;
/*
* Business 变量
*/
private String name;
public MySprite(String name) {
this.name = name;
this.thread = new Thread(this);
}
/**
* 启动线程
*/
public void start() {
running = true;
thread.start();
}
/**
* 挂起线程
*/
public void suspend() {
if (waiting) { // 是挂起状态则直接返回
return;
}
synchronized (this) {
this.waiting = true;
}
}
/**
* 恢复线程
*/
public void resume() {
if (!waiting) { // 没有挂起则直接返回
return;
}
synchronized (this) {
this.waiting = false;
this.notifyAll();
}
}
/**
* 停止线程
*/
public void stop() {
if (!running) { // 没有运行则直接返回
return;
}
synchronized (this) {
running = false;
}
}
public void run() {
for(;;) {
try {
// 线程挂起和退出处理
synchronized (this) {
if (!running) {
break;
}
if (waiting) {
this.wait();
}
}
// 应该做的事情
cry();
// 进入等待状态
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void cry() {
System.out.println(name + ":woo!");
}
}
可以结合UDP,TCP进行网络编程使用。在使用UDP时,由于DatagramSocket在接收数据的时候需要等待,程序不能进行停止的操作,所以要给datagramSocket加一个定时器,超时则进行下一次接收的过程。
本文介绍了一个基于Java实现的线程控制示例,通过自定义的MySprite类实现了线程的启动、挂起、恢复及停止操作。此外,文章还提到了如何将这种线程控制机制应用于网络编程中,特别是在使用UDP时如何处理DatagramSocket的等待问题。

被折叠的 条评论
为什么被折叠?



