php多线程 static变量,private static和public static的比较:多线程间
private static和public static的比較,區(qū)別在于修改的范圍不同,但作用域都是全局的即整個進程內(nèi)共享,與具體線程無關(guān)。
以下是測試代碼:
聲明靜態(tài)內(nèi)部變量的抽象類
package atest.privateStatic;
public abstract class TypeObject {
private static int id = 0;???//?private static?變量 by hiya
private int typeId;
private String word;
protected TypeObject(String word){
id = id + 1;
this.typeId = id;
this.word = word;
System.out.println("id=" + id + "? word=" + word);
}
}
// 具體實現(xiàn)類
package atest.privateStatic;
public class StringType extends TypeObject {
protected StringType(String word) {
super(word);
// TODO 自動生成されたコンストラクター?スタブ
}
}
多線程間測試類
package atest.privateStatic;
public class PrivateStaticTest {
/**
* [メソッドの説明を書きましょう]
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成されたメソッド?スタブ
PrivateStaticTest a = new PrivateStaticTest();
Test1 test1 = a.new Test1();
Thread t1 = new Thread(test1);
t1.start();
try {
Thread.sleep(1000);
} catch (Exception e){}
//??????? try {
//??????????? a.wait();
//??????? } catch (InterruptedException e) {
//??????????? // TODO 自動生成された catch ブロック
//??????????? e.printStackTrace();
//??????? }
PrivateStaticTest b = new PrivateStaticTest();
Test2 test2 = b.new Test2();
Thread t2 = new Thread(test2);
t2.start();
}
class Test1 implements Runnable{
public void run() {
for (int i = 0; i??????????????? StringType a1 = new StringType("h1h");
//StringType a2 = new StringType("h2h");
}
}
}
class Test2 implements Runnable{
public void run() {
for (int i = 0; i??????????????? StringType a1 = new StringType("m1m");
//StringType a2 = new StringType("m2m");
}
}
}
}
測試結(jié)果
http://hiyachen.blog.chinaunix.net
id=1? word=h1h
id=2? word=h1h
id=3? word=h1h
id=4? word=h1h
id=5? word=h1h
id=6? word=h1h
id=7? word=h1h
id=8? word=h1h
id=9? word=h1h
id=10? word=h1h
id=11? word=m1m
id=12? word=m1m
id=13? word=m1m
id=14? word=m1m
id=15? word=m1m
id=16? word=m1m
id=17? word=m1m
id=18? word=m1m
id=19? word=m1m
id=20? word=m1m
總結(jié)
以上是生活随笔為你收集整理的php多线程 static变量,private static和public static的比较:多线程间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mvc 怎么把后台拼接好的div写到前台
- 下一篇: 点击button后改变文字_27. 教你