求教下大家,是不是一个类只能创建一个main线程和一个run为入口的线程?如果我要为其他代码创建一个线程,就必须新建一个类然后再附带一个run方法?最后在main中创建不同类的实例去创建不同代码的线程?
如果要在一个类里创建若干个不同代码的线程要如何做?感觉start()的入口只有一个,run()...
如果要在一个类里创建若干个不同代码的线程要如何做?感觉start()的入口只有一个,run()...
解决方案 »
- 观察者模式
- 这里需要写两次pstm.close()有必要吗?
- java元数据的一点疑惑
- 两个List变量怎么刻隆(clone())!!在线等。。。。。。
- 哪位老大能给介绍一下LOGIMASTER 90 这个编程软件?
- JAVA是不是还不能做出这个效果来???如果未在表格一行的最后单元格按下回车键,就让回车动作等于TAB动作,反之到了最后单元格按下回车
- java中的return 0 和c中的return 0有什么不同吗?
- 现在csdn上没有人情味了
- 一个简单的问题。谁能帮助解决,一定送分
- 谁有JAVA的字节码结构方面的东东啊!请发给我
- 用jdk1.6,怎么让button变成透明
- pain方法的使用
static int x=0;
public static class mythread1 implements Runnable{
threadExam commonObj;
public mythread1(threadExam obj){
commonObj=obj;
}
public void run(){
while(x<10){
System.out.println(Thread.currentThread().getName()+",value of x is:"+commonObj.x);
x++;
} }
}
public static class mythread2 implements Runnable{
threadExam commonObj;
public mythread2(threadExam obj){
commonObj=obj;
}
public void run(){
x=x-1;
System.out.println(Thread.currentThread().getName()+",x="+commonObj.x);
}
public static void main(String []args){
threadExam d=new threadExam();
mythread1 r1=new mythread1(d);
Thread t1=new Thread(r1,"Pig");
mythread2 r2=new mythread2(d);
Thread t2=new Thread(r2);
t2.setName("Rabbit");
t1.start();
t2.start();
}
}
}被抛出:
java.lang.NoSuchMethodError: main
Exception in thread "main"
的异常,能帮看下错在哪么?
static int x=0;
public static class mythread1 implements Runnable{
threadExam commonObj;
public mythread1(threadExam obj){
commonObj=obj;
}
public void run(){
while(x<10){
System.out.println(Thread.currentThread().getName()+",value of x is:"+commonObj.x);
x++;
}
}
}
public static class mythread2 implements Runnable{
threadExam commonObj;
public mythread2(threadExam obj){
commonObj=obj;
}
public void run(){
x=x-1;
System.out.println(Thread.currentThread().getName()+",x="+commonObj.x);
}
}
public static void main(String []args){
threadExam d=new threadExam();
mythread1 r1=new mythread1(d);
Thread t1=new Thread(r1,"Pig");
mythread2 r2=new mythread2(d);
Thread t2=new Thread(r2);
t2.setName("Rabbit");
t1.start();
t2.start();
}}
可以了,原因是大括号不匹配...哎,习惯不好啊~