当前位置: 技术问答>java相关
(新手)请教这个线程的错误在哪?
来源: 互联网 发布时间:2015-06-22
本文导语: import java.io.*; public class WithThread { static PrintWriter out = new PrintWriter(System.out,true); public static void main(String args[]) { //first task:some pseudo-I/O operation ThreadPseudoIO pseudo = new ThreadPseudoIO(); /* 上一行出错信息: ...
import java.io.*;
public class WithThread
{
static PrintWriter out = new PrintWriter(System.out,true);
public static void main(String args[])
{
//first task:some pseudo-I/O operation
ThreadPseudoIO pseudo = new ThreadPseudoIO();
/* 上一行出错信息:
non-static variable this cannot be referenced from a static context
错误箭头指在 new 上
*/
pseudo.start();
//second task:some random task
showElapsedTime("Another task starts");
}
static long baseTime = System.currentTimeMillis();
//show the time elapsed since the program started
static void showElapsedTime(String message)
{
long elapsedTime=System.currentTimeMillis()-baseTime;
out.println(message + " at "+ (elapsedTime/1000.0) + " seconds");
}
//pseudo-I/O operation run in a separate thread
class ThreadPseudoIO extends Thread
{
int data=-1;
ThreadPseudoIO()
{
//constructor
WithThread.showElapsedTime("ThreadPseudoIO created");
}
public void run()
{
WithThread.showElapsedTime("ThreadPseudoIO starts");
try
{
Thread.sleep(10000);
data=999;
WithThread.showElapsedTime("ThreadPseudoIO finishes");
}
catch(InterruptedException e){}
}
}
}
public class WithThread
{
static PrintWriter out = new PrintWriter(System.out,true);
public static void main(String args[])
{
//first task:some pseudo-I/O operation
ThreadPseudoIO pseudo = new ThreadPseudoIO();
/* 上一行出错信息:
non-static variable this cannot be referenced from a static context
错误箭头指在 new 上
*/
pseudo.start();
//second task:some random task
showElapsedTime("Another task starts");
}
static long baseTime = System.currentTimeMillis();
//show the time elapsed since the program started
static void showElapsedTime(String message)
{
long elapsedTime=System.currentTimeMillis()-baseTime;
out.println(message + " at "+ (elapsedTime/1000.0) + " seconds");
}
//pseudo-I/O operation run in a separate thread
class ThreadPseudoIO extends Thread
{
int data=-1;
ThreadPseudoIO()
{
//constructor
WithThread.showElapsedTime("ThreadPseudoIO created");
}
public void run()
{
WithThread.showElapsedTime("ThreadPseudoIO starts");
try
{
Thread.sleep(10000);
data=999;
WithThread.showElapsedTime("ThreadPseudoIO finishes");
}
catch(InterruptedException e){}
}
}
}
|
应该把class ThreadPseudoIO extends Thread
改成 static class ThreadPseudoIO extends Thread
试试看!
改成 static class ThreadPseudoIO extends Thread
试试看!
|
在静态函数中不能使用非静态变量.
将ThreadPseudoIO pseudo = new ThreadPseudoIO();
改成
static ThreadPseudoIO pseudo = new ThreadPseudoIO();
将ThreadPseudoIO pseudo = new ThreadPseudoIO();
改成
static ThreadPseudoIO pseudo = new ThreadPseudoIO();
|
ThreadPseudoIO pseudo = new ThreadPseudoIO();
ThreadPseudoIO 不是一个静态内部类。
使用:WithThread.ThreadPseudoIO pseudo = new WithThread.ThreadPseudoIO(); 试试看
ThreadPseudoIO 不是一个静态内部类。
使用:WithThread.ThreadPseudoIO pseudo = new WithThread.ThreadPseudoIO(); 试试看
|
感觉整个程序乱78糟的
才刚开始学呢就不要学着人家这个类套着那个类的,把自己搞的糊涂了吧
才刚开始学呢就不要学着人家这个类套着那个类的,把自己搞的糊涂了吧
|
程序的可读性太差,建议先修改程序在调试