java 线程启动不了 求大神解决下谢谢

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 线程启动不了 求大神解决下谢谢相关的知识,希望对你有一定的参考价值。

import java.awt.*;
import java.awt.Graphics;
import javax.swing.*;

public class test1
public static void main(String[] args)
JFrame frm = new JFrame("Java Farme");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ball B1=new ball(50,50);
frm.add(B1);
B1.start();
frm.setBounds(400,200,700,400);
frm.setVisible(true);



class ball extends JPanel implements Runnable
int x;
int y;
public ball(int x,int y)
this.x=x;
this.y=y;

public void paint(Graphics g)
super.paint(g);
g.setColor(Color.blue);
g.fillOval(x,y,40,40);


public void run()
for(;;)
if(x<400)
x+=50;
this.repaint();


else
x=0;
this.repaint();


try
Thread.sleep(100);

catch (InterruptedException ex)
System.err.println(ex.toString());



//修改了,添加了start、stop方法、并且在run方法中添加判断。。。

import java.awt.*;
import javax.swing.*;
public class test1 
    public static void main(String[] args) 
        JFrame frm = new JFrame("Java Farme");
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ball B1 = new ball(50, 50);
        frm.add(B1);
        B1.start();
        frm.setBounds(400, 200, 700, 400);
        frm.setVisible(true);
    

class ball extends JPanel implements Runnable 
    int x;
    int y;
    Thread thread;
    public ball(int x, int y) 
        this.x = x;
        this.y = y;
    
    public void paint(Graphics g) 
        super.paint(g);
        g.setColor(Color.blue);
        g.fillOval(x, y, 40, 40);
        g.dispose();
    
    public void start() 
        if (null == thread) 
            thread = new Thread(this);
            thread.start();
        
    
    public synchronized void stop() 
        if (null != thread) 
            thread.interrupt();
            thread = null;
            notifyAll();
        
    
    public void run() 
        Thread me = Thread.currentThread();
        for (; me == thread;) 
            if (x < 400) 
                x += 50;
                this.repaint();
             else 
                x = 0;
                this.repaint();
            
            try 
                Thread.sleep(100);
             catch (InterruptedException ex) 
        
    

参考技术A 把B1.start();
改为new Thread(B1).start();
参考技术B 发发汗i 参考技术C B1.start();
你那里有start()方法,你只有run()方法追问

应该怎么改?🌞

参考技术D 梦李白·其一(杜甫)

Java如何实现线程的暂停和重新启用?求大神

JAVA中线程开始有start方法,暂停用sleep(time)方法,线程停止用stop方法,线程等待wait方法,java 中没有线程重启一说,只能说线程唤醒notifyAll()或是notify方法,前一个notifyAll()方法是唤醒所有的已休眠或是等待状态下的线程。具体的一种参数请参照JDK文档。

Java中的线程的生命周期大体可分为5种状态。如下:

1.新建(NEW):新创建了一个线程对象。

2.可运行(RUNNABLE):线程对象创建后,其他线程(比如main线程)调用了该对象的start()方法。该状态的线程位于可运行线程池中,等待被线程调度选中,获取cpu 的使用权 。

3.运行(RUNNING):可运行状态(runnable)的线程获得了cpu 时间片(timeslice) ,执行程序代码。

4.阻塞(BLOCKED):阻塞状态是指线程因为某种原因放弃了cpu 使用权,也即让出了cpu timeslice,暂时停止运行。直到线程进入可运行(runnable)状态,才有机会再次获得cpu timeslice 转到运行(running)状态。阻塞的情况分三种: 

(一).等待阻塞:运行(running)的线程执行o.wait()方法,JVM会把该线程放入等待队列(waitting queue)中。

(二).同步阻塞:运行(running)的线程在获取对象的同步锁时,若该同步锁被别的线程占用,则JVM会把该线程放入锁池(lock pool)中。

(三).其他阻塞:运行(running)的线程执行Thread.sleep(long ms)或t.join()方法,或者发出了I/O请求时,JVM会把该线程置为阻塞状态。当sleep()状态超时、join()等待线程终止或者超时、或者I/O处理完毕时,线程重新转入可运行(runnable)状态。

5.死亡(DEAD):线程run()、main() 方法执行结束,或者因异常退出了run()方法,则该线程结束生命周期。死亡的线程不可再次复生。

参考技术A

  曾经碰到一个问题,线程的暂停与继续,想了想,去使用JDK给我们提供的suspend方法、interrupt方法??suspend()方法让这个线程与主线程都暂停了,谁来唤醒他们??明显这个不好用,要用的话,恐怕得另写唤醒线程了!interrupt方法,这个方法实际上只能中断当前线程!

        既然JDK解决不了偶的问题,偶只能自己写了!

        这个时候想到了Object的wait()和notifyAll()方法。使用这两个方法让线程暂停,并且还能恢复,我只需要封装一下,就能够变成非常之好用的代码了!如下请看:

 

        新建Thread类继承MyThread只需实现runPersonelLogic()即可跑你自己的逻辑啦!!!

    

        另外调用setSuspend(true)是当前线程暂停/ 等待,调用setSuspend(false)让当前线程恢复/唤醒!自我感觉很好使!

public abstract class MyThread extends Thread 

private boolean suspend = false;

private String control = ""; // 只是需要一个对象而已,这个对象没有实际意义

public void setSuspend(boolean suspend) 
if (!suspend) 
synchronized (control) 
control.notifyAll();


this.suspend = suspend;


public boolean isSuspend() 
return this.suspend;


public void run() 
while (true) 
synchronized (control) 
if (suspend) 
try 
control.wait();
 catch (InterruptedException e) 
e.printStackTrace();



this.runPersonelLogic();



protected abstract void runPersonelLogic();

public static void main(String[] args) throws Exception 
MyThread myThread = new MyThread() 
protected void runPersonelLogic() 
System.out.println("myThead is running");

;
myThread.start();
Thread.sleep(3000);
myThread.setSuspend(true);
System.out.println("myThread has stopped");
Thread.sleep(3000);
myThread.setSuspend(false);

参考技术B 让线程睡眠,调用线程的sleep(timeout)方法。追问

能给个实例么?

参考技术C JAVA中线程开始有start方法,暂停用sleep(time)方法,线程停止用stop方法,线程等待wait方法,java 中没有线程重启一说,只能说线程唤醒notifyAll()或是notify方法,前一个notifyAll()方法是唤醒所有的已休眠或是等待状态下的线程。具体的一种参数请参照JDK文档。打字不易,望采纳。追问

能给个实例么?

追答

等等啊,给你找找

追问

好的,多谢啦!

追答

给我你的邮箱,我发给你

追问

发私信吧

追答

我这里有视频,全方位的讲解多线程技术,两百多M,邮件比较好。

追问

企鹅3086642601

本回答被提问者采纳
参考技术D 那resume呢

以上是关于java 线程启动不了 求大神解决下谢谢的主要内容,如果未能解决你的问题,请参考以下文章