2013年11月18日星期一

java Integer not being operated lock it ?

public class TestVolitile extends Thread {

TestVolitile(int id) {
this.setName("thread :" + id);
}

public static Integer n = new Integer(0);
public static byte[] lock=new byte[0];

public void run() {
synchronized (n) {
// System.err.println(getName() + "  n " + n);
for (int i = 0; i < 3; i++) {
n++;
try {
sleep(1);
} catch (InterruptedException e) {
} // 为了使运行结果更随机,延迟3毫秒
}
// System.err.println(getName() + "  end " + n);
}
}

static void runTest() throws InterruptedException{
Thread threads[] = new Thread[1000];
for (int i = 0; i < threads.length; i++)
// 建立100个线程
threads[i] = new TestVolitile(i);
for (int i = 0; i < threads.length; i++)
// 运行刚才建立的100个线程
threads[i].start();
for (int i = 0; i < threads.length; i++)
// 100个线程都执行完后继续
threads[i].join();
System.out.println(" n= " + TestVolitile.n);
TestVolitile.n=0;
}

public static void main(String[] args) throws Exception {
for (int i = 0; i < 50; i++) {
runTest();
}
}
}

n
Why not lock , for a lock on it too ?
------ Solution ---------------------------------------- ----
you use public static byte [] lock = new byte [0] like ? Why ? Because you are in the code , did not modify this variable , public static byte [] lock = new byte [0] and I wrote above
private Object obj = new Object ();
synchronized (obj)
and
synchronized (this)
no distinction , are objects of the built-in lock. Sync it are thread safe. Your
public static Integer n = new Integer (0);
by you for n + +; operation modified.

------ For reference only ---------------------------------- -----
Integer is final, + + operator will regenerate an object or value.
n = n +1; like to try it ?
------ For reference only -------------------------------------- -

Well, I was wrong. . n + + and n = n +1 the same effect. .
------ For reference only ---------------------------------------
really do not understand what you write What is ? Your program want to do ?
launched 50,000 threads, which is to be doing it ? Did not understand
------ For reference only ----------------------------------- ----


is to test multithreaded lock , synchronized (n) {......}

began to use public static Integer n = new Integer (0); this object when the lock and found that does not work, then replace public static ; byte [] lock = new byte [0]; object , like, do not know which is operated in the thread object n, can not be used as a lock object ?
------ For reference only -------------------------------------- -

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class Test01 implements Runnable {
private String name;
private TestThread t;
private AtomicInteger count = new AtomicInteger();

public Test01(String name, TestThread t) {
super();
this.name = name;
this.t = t;
}

@Override
public void run() {
Thread.currentThread().setName(name);
while (true) {
t.showCount();
if (10 > count.getAndIncrement()) {
break;
}
}
}

/**
 * @param args
 */
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
TestThread t = new TestThread();
for (int i = 0; i < Runtime.getRuntime().availableProcessors() * 4; i++) {
exec.execute(new Test01("thread-" + i, t));
}
exec.shutdown();

}
}

class TestThread {
private Object obj = new Object();
private int count;

public void showCount() {
synchronized (obj) {
//synchronized (this) {//这里可以用obj,可以用this
try {
System.out.println(Thread.currentThread().getName()
+ " before --> " + count);
synchronized (obj) {
count++;
}
System.out.println(Thread.currentThread().getName()
+ " after --> " + count);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}


look at this code .
I use here the object's internal lock to ensure thread safety. Can I use my above

private Object obj = new Object();
synchronized (obj)

You can also use

synchronized (this)

effect of the two is the same. Of course, you can also use the display
private Lock lock = new ReentrantLock();

to lock and unlock .
you use public static Integer n = new Integer (0); when, in the back of the implementation of n + +; operation. You have to understand , n + + This operation , in fact, is the production of a new n, then n is assigned to . Your lock " invariance " has long ceased to exist. You may want to print out n, not long ago the beginning of the n it. Go and see "Java concurrent programming combat " it.
------ For reference only -------------------------------------- -
6 Floor positive solution, is reading that book , ha ha , thank you

没有评论:

发表评论