Java 请帮帮忙,下面这个程序错在哪?
importorg.omg.PortableServer.ThreadPolicyOperations;/**Tochangethistemplate,chooseToo...
import org.omg.PortableServer.ThreadPolicyOperations;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author stud
*/
public class Java_2 extends Thread{
public static void main(String args[]) {
Hello h = new Hello();
//*********Found********
Thread t = Thread(h);
t.start();
}
}
//*********Found********
class Hello implements ThreadPolicy {
int i = 0;
public void run() {
while (true) {
System.out.println("Hello!");
if (i++ == 5) {
break;
}
}
}
} 展开
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author stud
*/
public class Java_2 extends Thread{
public static void main(String args[]) {
Hello h = new Hello();
//*********Found********
Thread t = Thread(h);
t.start();
}
}
//*********Found********
class Hello implements ThreadPolicy {
int i = 0;
public void run() {
while (true) {
System.out.println("Hello!");
if (i++ == 5) {
break;
}
}
}
} 展开
3个回答
展开全部
有几个问题:
1. Thread t = Thread(h); 这行少了new
2. Hello要implement Runnable,才能放到Thread()里面(也就是上面第1点提到的那个行那种用法)
3. ThreadPolicy的很多其他method没有实现。我下面的代码加了,但是每个method里面没实际代码
下面程序运行结果为:
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
import org.omg.CORBA.Context;
import org.omg.CORBA.ContextList;
import org.omg.CORBA.DomainManager;
import org.omg.CORBA.ExceptionList;
import org.omg.CORBA.NVList;
import org.omg.CORBA.NamedValue;
import org.omg.CORBA.Object;
import org.omg.CORBA.Policy;
import org.omg.CORBA.Request;
import org.omg.CORBA.SetOverrideType;
import org.omg.PortableServer.ThreadPolicy;
import org.omg.PortableServer.ThreadPolicyValue;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author stud
*/
public class Java_2 extends Thread{
public static void main(String args[]) {
Hello h = new Hello();
//*********Found********
Thread t = new Thread(h);
t.start();
}
}
//*********Found********
class Hello implements ThreadPolicy, Runnable {
/**
*
*/
private static final long serialVersionUID = -112603238151033723L;
int i = 0;
public void run() {
while (true) {
System.out.println("Hello!");
if (i++ == 5) {
break;
}
}
}
public ThreadPolicyValue value() {
// TODO Auto-generated method stub
return null;
}
public Policy copy() {
// TODO Auto-generated method stub
return null;
}
public void destroy() {
// TODO Auto-generated method stub
}
public int policy_type() {
// TODO Auto-generated method stub
return 0;
}
public Request _create_request(Context arg0, String arg1, NVList arg2,
NamedValue arg3) {
// TODO Auto-generated method stub
return null;
}
public Request _create_request(Context arg0, String arg1, NVList arg2,
NamedValue arg3, ExceptionList arg4, ContextList arg5) {
// TODO Auto-generated method stub
return null;
}
public Object _duplicate() {
// TODO Auto-generated method stub
return null;
}
public DomainManager[] _get_domain_managers() {
// TODO Auto-generated method stub
return null;
}
public Object _get_interface_def() {
// TODO Auto-generated method stub
return null;
}
public Policy _get_policy(int arg0) {
// TODO Auto-generated method stub
return null;
}
public int _hash(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public boolean _is_a(String arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean _is_equivalent(Object arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean _non_existent() {
// TODO Auto-generated method stub
return false;
}
public void _release() {
// TODO Auto-generated method stub
}
public Request _request(String arg0) {
// TODO Auto-generated method stub
return null;
}
public Object _set_policy_override(Policy[] arg0, SetOverrideType arg1) {
// TODO Auto-generated method stub
return null;
}
}
1. Thread t = Thread(h); 这行少了new
2. Hello要implement Runnable,才能放到Thread()里面(也就是上面第1点提到的那个行那种用法)
3. ThreadPolicy的很多其他method没有实现。我下面的代码加了,但是每个method里面没实际代码
下面程序运行结果为:
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
import org.omg.CORBA.Context;
import org.omg.CORBA.ContextList;
import org.omg.CORBA.DomainManager;
import org.omg.CORBA.ExceptionList;
import org.omg.CORBA.NVList;
import org.omg.CORBA.NamedValue;
import org.omg.CORBA.Object;
import org.omg.CORBA.Policy;
import org.omg.CORBA.Request;
import org.omg.CORBA.SetOverrideType;
import org.omg.PortableServer.ThreadPolicy;
import org.omg.PortableServer.ThreadPolicyValue;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author stud
*/
public class Java_2 extends Thread{
public static void main(String args[]) {
Hello h = new Hello();
//*********Found********
Thread t = new Thread(h);
t.start();
}
}
//*********Found********
class Hello implements ThreadPolicy, Runnable {
/**
*
*/
private static final long serialVersionUID = -112603238151033723L;
int i = 0;
public void run() {
while (true) {
System.out.println("Hello!");
if (i++ == 5) {
break;
}
}
}
public ThreadPolicyValue value() {
// TODO Auto-generated method stub
return null;
}
public Policy copy() {
// TODO Auto-generated method stub
return null;
}
public void destroy() {
// TODO Auto-generated method stub
}
public int policy_type() {
// TODO Auto-generated method stub
return 0;
}
public Request _create_request(Context arg0, String arg1, NVList arg2,
NamedValue arg3) {
// TODO Auto-generated method stub
return null;
}
public Request _create_request(Context arg0, String arg1, NVList arg2,
NamedValue arg3, ExceptionList arg4, ContextList arg5) {
// TODO Auto-generated method stub
return null;
}
public Object _duplicate() {
// TODO Auto-generated method stub
return null;
}
public DomainManager[] _get_domain_managers() {
// TODO Auto-generated method stub
return null;
}
public Object _get_interface_def() {
// TODO Auto-generated method stub
return null;
}
public Policy _get_policy(int arg0) {
// TODO Auto-generated method stub
return null;
}
public int _hash(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public boolean _is_a(String arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean _is_equivalent(Object arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean _non_existent() {
// TODO Auto-generated method stub
return false;
}
public void _release() {
// TODO Auto-generated method stub
}
public Request _request(String arg0) {
// TODO Auto-generated method stub
return null;
}
public Object _set_policy_override(Policy[] arg0, SetOverrideType arg1) {
// TODO Auto-generated method stub
return null;
}
}
展开全部
看你这个结构应该是想使用多线程的
但是你这里 用实现ThreadPolicy来完成多线程我就不明白你什么意思了
不妨将这段代码改成这样:
public static void main(String args[]) {
Hello h = new Hello();
Thread t = new Thread(h);
t.start();
}
}
class Hello implements Runnable {
int i = 0;
public void run() {
while (true) {
System.out.println("Hello!");
if (i++ == 5) {
break;
}
}
}
但是你这里 用实现ThreadPolicy来完成多线程我就不明白你什么意思了
不妨将这段代码改成这样:
public static void main(String args[]) {
Hello h = new Hello();
Thread t = new Thread(h);
t.start();
}
}
class Hello implements Runnable {
int i = 0;
public void run() {
while (true) {
System.out.println("Hello!");
if (i++ == 5) {
break;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Thread t = Thread(h);要改成Thread t =new Thread(h);
然后在t.start();后面加上t.join();
不然主线程执行完t.start就结束了,程序也就随即结束了,等不到t线程输出
然后在t.start();后面加上t.join();
不然主线程执行完t.start就结束了,程序也就随即结束了,等不到t线程输出
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |