2013年9月23日星期一

Please Daren advice , multi-threaded concurrent reads


package tt;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ParaTest {
/**
 * @param args
 */
public static void main(String[] args) throws Exception {

    /**
     *  目的: 比较两种方法的速率快慢
     *  第一种方法: 单独一个线程串行的向日志文件中写入2048个元素
     *  第二种方法: 两个线程并行,一个线程生成2048个元素后,另外一个获取该容器的锁,并写入日志文件。
     *      
     */

/**
 * case 1:
 */
PThread_1 p1 = new PThread_1();
Thread t1 = new Thread(p1);
long startTime = System.currentTimeMillis();
t1.start();
t1.join();
long endTime = System.currentTimeMillis();
System.out.println("The T1 running time is :" + (endTime - startTime));

/**
 * case 2:
 */
PThread_2 p2 = new PThread_2();
PThread_3 p3 = new PThread_3();
Thread t2 = new Thread(p2);
Thread t3 = new Thread(p3);
t2.start();
t2.join();

startTime = System.currentTimeMillis();
  t3.start();
  t3.join();
endTime = System.currentTimeMillis();
System.out.println("The T3 running time is :"
+ (endTime - startTime));
}

}

class PThread_1 implements Runnable {

public static List<Long> lt = new ArrayList<Long>();
public static final Log pThread_1 = LogFactory.getLog(PThread_1.class);

PThread_1() {
}

@Override
public void run() {
// TODO Auto-generated method stub
while (lt.size() < 2048) {
lt.add(System.currentTimeMillis());
}

for (int i = 0; i < 2048 ; i++) {
pThread_1.info(lt.get(i));
}
 
lt.clear();
}
}

class PThread_2 implements Runnable {

public static List<Long> lt = new ArrayList<Long>();

PThread_2() {
}

@Override
public void run() {
synchronized (lt) {
while (lt.size() < 2048) {
lt.add(System.currentTimeMillis());
}
// lt.notifyAll();
}
}
}

class PThread_3 implements Runnable {

public static final Log pThread_3 = LogFactory.getLog(PThread_3.class);

PThread_3() {
}

@Override
public void run() {
// TODO Auto-generated method stub
synchronized (PThread_2.lt) {
// try {
// if(PThread_2.lt.size()<2048)
// PThread_2.lt.wait();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }

while (PThread_2.lt.size() >= 2048) {
for (int i = 0; i < PThread_2.lt.size(); i++)
pThread_3.info(PThread_2.lt.get(i));
}
PThread_2.lt.clear();
}
}
}


log4j configuration file is as follows:


log4j.rootLogger=WARN

log4j.logger.pThread_1=INFO,p1
log4j.appender.p1=org.apache.log4j.FileAppender
log4j.appender.p1.File=D://log4j/pThread_1.log 
log4j.appender.p1.layout=org.apache.log4j.PatternLayout
log4j.appender.p1.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.logger.pThread_3=INFO,p3
log4j.appender.p3=org.apache.log4j.FileAppender
log4j.appender.p3.File=D://log4j/pThread_3.log 
log4j.appender.p3.layout=org.apache.log4j.PatternLayout
log4j.appender.p3.layout.ConversionPattern=%d %p [%c] - %m%n


two problems :
1. t3 thread behind the delay in the end .
2. two are not the contents of the log file output .
I do not know what the reasons are . Great God seeking guidance.
------ Solution ---------------------------------------- ----
PThread_3 a while did not enter it, you PThread_2 a while the cycle is dead , right ? lt.size () == 0
------ Solution ---------------------------- ----------------
 roof . . .
------ Solution ---------------------------------------- ----

  
PThread_3 synchronization part wrong, change if enough   
if (PThread_2.lt.size ()> = 2048) {   
for (int i = 0; i pThread_3.info (PThread_2.lt.get (i));   
}   
  
just log or not the content written into ...  

Oh, right right.
log your side is LogFactory.getLog (PThread_3.class) obtained , PThread_3.class is the complete package path , then your configuration file should log4j.logger. package path
------ For reference only ---------------------------------------


loop every time plus one , ah !
while (lt.size () <2048) {
lt.add (System.currentTimeMillis ());
}

------ For reference only ---------------------------------- -----


PThread_3 synchronization part wrong, change if enough
if (PThread_2.lt.size ()> = 2048) {
for (int i = 0; i pThread_3.info (PThread_2.lt.get (i));
}

just log or not the content written into ...
------ For reference only ------------------------ ---------------
I remember how log4j itself on multiple threads to synchronize concurrent writes a
------ For reference only ---- -----------------------------------

    
PThread_3 synchronization part wrong, change if enough     
if (PThread_2.lt.size ()> = 2048) {     
for (int i = 0; i pThread_3.info (PThread_2.lt.get (i));     
}     
    
just log or not the content written into ...          
  
Oh, right right.   
log your side is LogFactory.getLog (PThread_3.class) obtained , PThread_3.class is the complete package path , then your configuration file should log4j.logger. package path  
Yes

没有评论:

发表评论