当前位置: 技术问答>java相关
大家快来看看,急死我了!在线等待^^^^^^^^^
来源: 互联网 发布时间:2017-03-22
本文导语: 想写一个计数器,用四个线程来让它+1,原程序如下: import java.io.*; public class Coun { public static void main(String args[]){ new Counter("A"); new Counter("B"); new Counter("C"); new Counter("D"); try{Thread....
想写一个计数器,用四个线程来让它+1,原程序如下:
import java.io.*;
public class Coun {
public static void main(String args[]){
new Counter("A");
new Counter("B");
new Counter("C");
new Counter("D");
try{Thread.sleep(10000);}
catch(InterruptedException e){}
}
}
class Counter extends Thread{
CounReload load;
String name;
public static int count=0;
Counter(String name){
this.name=name;
new Thread(this,"dfds").start();
}
public void run(){
while(true){
System.out.print(name+" ");
load.change(count);
try{Thread.sleep(100);}
catch(InterruptedException e){}
}
}
}
class CounReload{
int count;
synchronized int change(int count){
this.count=count;
count++;
System.out.println("count: "+count);
return count;
}
}
java Coun
结果:
B java.lang.NullPointerExceptionC D A
at Counter.run(Coun.java:26)
at java.lang.Thread.run(Thread.java:539)
java.lang.NullPointerException
at Counter.run(Coun.java:26)
at java.lang.Thread.run(Thread.java:539)
java.lang.NullPointerException
at Counter.run(Coun.java:26)
at java.lang.Thread.run(Thread.java:539)
java.lang.NullPointerException
at Counter.run(Coun.java:26)
at java.lang.Thread.run(Thread.java:539)
这是什么意思呀!
真是搞不明白!
把public static int count=0;放到Coun类中它又说在load.change(count)中找不到count这个变量真不知道怎么才行了.
import java.io.*;
public class Coun {
public static void main(String args[]){
new Counter("A");
new Counter("B");
new Counter("C");
new Counter("D");
try{Thread.sleep(10000);}
catch(InterruptedException e){}
}
}
class Counter extends Thread{
CounReload load;
String name;
public static int count=0;
Counter(String name){
this.name=name;
new Thread(this,"dfds").start();
}
public void run(){
while(true){
System.out.print(name+" ");
load.change(count);
try{Thread.sleep(100);}
catch(InterruptedException e){}
}
}
}
class CounReload{
int count;
synchronized int change(int count){
this.count=count;
count++;
System.out.println("count: "+count);
return count;
}
}
java Coun
结果:
B java.lang.NullPointerExceptionC D A
at Counter.run(Coun.java:26)
at java.lang.Thread.run(Thread.java:539)
java.lang.NullPointerException
at Counter.run(Coun.java:26)
at java.lang.Thread.run(Thread.java:539)
java.lang.NullPointerException
at Counter.run(Coun.java:26)
at java.lang.Thread.run(Thread.java:539)
java.lang.NullPointerException
at Counter.run(Coun.java:26)
at java.lang.Thread.run(Thread.java:539)
这是什么意思呀!
真是搞不明白!
把public static int count=0;放到Coun类中它又说在load.change(count)中找不到count这个变量真不知道怎么才行了.
|
/***
名称和 count 的数值有时会错开, 你可能需要吧名字输出也加到同步方法中去.
**/
import java.io.*;
public class T1 {
public static void main(String args[]){
CounReload load = new CounReload();
new Counter(load, "A");
new Counter(load, "B");
new Counter(load, "C");
new Counter(load, "D");
try{
Thread.sleep(10000);
}catch(InterruptedException e){
}
System.exit(0);
}
}
class Counter extends Thread{
CounReload load;
String name;
Counter(CounReload load , String name){
this.name=name;
this.load = load;
new Thread(this,"dfds").start();
}
public void run(){
while(true){
System.out.print(name+" ");
load.change();
try{
Thread.sleep(100);
}catch(InterruptedException e){
}
}
}
}
class CounReload{
// define count here
int count = 0;
// synchronized method
synchronized int change(){
count++;
System.out.println("count: "+count);
return count;
}
}
名称和 count 的数值有时会错开, 你可能需要吧名字输出也加到同步方法中去.
**/
import java.io.*;
public class T1 {
public static void main(String args[]){
CounReload load = new CounReload();
new Counter(load, "A");
new Counter(load, "B");
new Counter(load, "C");
new Counter(load, "D");
try{
Thread.sleep(10000);
}catch(InterruptedException e){
}
System.exit(0);
}
}
class Counter extends Thread{
CounReload load;
String name;
Counter(CounReload load , String name){
this.name=name;
this.load = load;
new Thread(this,"dfds").start();
}
public void run(){
while(true){
System.out.print(name+" ");
load.change();
try{
Thread.sleep(100);
}catch(InterruptedException e){
}
}
}
}
class CounReload{
// define count here
int count = 0;
// synchronized method
synchronized int change(){
count++;
System.out.println("count: "+count);
return count;
}
}
|
你load没初始化,去调change肯定时Nullpointer了
|
倒了。
你这里的load.change(count);的这个load没有实例话,是个空对象阿。他不是着不到count而是找不到load。你可以这样改的
class Counter extends Thread{
CounReload load=new CounReload();
String name;
public static int count=0;
......
你这里的load.change(count);的这个load没有实例话,是个空对象阿。他不是着不到count而是找不到load。你可以这样改的
class Counter extends Thread{
CounReload load=new CounReload();
String name;
public static int count=0;
......
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。