/*代码原意是Game线程每隔0.3秒钟给int变量head加一,然后打印出来;TimeCounter线程在第1.5秒钟使Game线程等待3秒钟,然后Game线程重新运行,可是我的程序在运行时候Game线程并没有等待3秒钟,为什么呢?*/
class TimeCounter extends Thread { Game game; TimeCounter(Game g) {
game = g;
} public void run() {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (game != null)
game.stop();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (game != null)
game.resume();
}
}
class Game implements Runnable {
// 只写重要的代码 int head; Thread thread; public void run() { while (true) {
head++;
System.out.println(head);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public void start() {
thread = new Thread(this);
thread.start();
} public void stop() {
try {
thread.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void resume() {
thread.notify();
} public static void main(String[] args) { Game game = new Game();
TimeCounter tc = new TimeCounter(game);
game.start();
tc.start();
}
}
class TimeCounter extends Thread { Game game; TimeCounter(Game g) {
game = g;
} public void run() {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (game != null)
game.stop();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (game != null)
game.resume();
}
}
class Game implements Runnable {
// 只写重要的代码 int head; Thread thread; public void run() { while (true) {
head++;
System.out.println(head);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public void start() {
thread = new Thread(this);
thread.start();
} public void stop() {
try {
thread.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void resume() {
thread.notify();
} public static void main(String[] args) { Game game = new Game();
TimeCounter tc = new TimeCounter(game);
game.start();
tc.start();
}
}
if (isStopped) {
try {
synchronized (thread) {
thread.wait();
}
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} stop方法中
public void stop() {
isStopped = true;
}resume方法中
public void resume() {
synchronized(thread){
thread.notify();
isStopped = false;
}
}
其余不变
{ Game game; TimeCounter(Game g)
{
game = g;
} public void run()
{
try
{
Thread.sleep(1500);
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
if(game != null)
{
game.setStop(true);
}
}
}class Game implements Runnable
{
// 只写重要的代码 int head;
boolean stop;
Thread thread; public void run()
{
while(true)
{
synchronized(this)
{
head++;
System.out.println(head); try
{
if(stop)
{
this.wait(3000);
stop = false;
}
Thread.sleep(300);
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
}
} public void start()
{
thread = new Thread(this);
thread.start();
} public void setStop(boolean stop)
{
this.stop = stop;
} public static void main(String[] args)
{
Game game = new Game();
TimeCounter tc = new TimeCounter(game);
game.start();
tc.start();
}
}
比如:若程序需要 类似“脉冲”或“心跳” 的功能时,就要这样。
obj.wait(); //这里释放同步锁
}
出现了Sleep了,说明你的代码需要重新设计。那用什么办法让线程休息呢?
至于LZ的程序直接跑肯定有异常,因为wait方法要释放所有锁资源,所以它的调用前提是必须先获得对象锁资源,我修改了一下代码,可以跑了,不过现象很有趣,大家可以看看为什么会这样运行?是否知道真正的原因呢?
class TimeCounter extends Thread {
private Game game; public TimeCounter(Game g){
game = g;
} public void run(){
try{
System.out.println("Counter waiting...");
Thread.sleep(1500);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("stop begin...");
if(game != null)
game.stop();
System.out.println("3000 begin...");
try{
Thread.sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}
if(game != null)
game.resume();
}
} public class Game implements Runnable{
private int head;
public void run(){
while(true){
head++;
System.out.println("Head:"+head);
try{
Thread.sleep(300);
}catch(InterruptedException e){
e.printStackTrace();
}
}
} public void start(){
new Thread(this).start();
}
public synchronized void stop(){
try{
System.out.println("waiting...");
this.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
} public void resume(){
System.out.println("notify...");
notify();
}
public static void main(String[] args){
Game game = new Game();
TimeCounter tc = new TimeCounter(game);
game.start();
tc.start();
}
} //打印结果
Head:1
Counter waiting...
Head:2
Head:3
Head:4
Head:5
stop begin...
waiting...
Head:6
Head:7
Head:8
Head:9
Head:10
Head:11
Head:12
Head:13
Head:14
...一直Head下去了
稍后我公布答案哦……确实需要很多Java线程知识!
this.wait(); 这里停的并非 new Thread(this).start(); 这里产生的线程但这好像不是楼主提出的问题吧
class Game implements Runnable
{
// 只写重要的代码 int head;
boolean stop;
Thread thread; public void run()
{
while(true)
{
synchronized(this)
然后就像13楼说的,那个线程假设是T1停了,而再没有被唤醒过……希望大家好好理解线程这个东东,呵呵!