发表于: 2018-01-08 23:40:00

1 571



今天完成的事情:

1.  学习多线程

2. 上传文件接口,上传文件调通


明天计划的事情

1. 小课堂

2. 文件上传和下载


遇到的问题:

1.  IDEA 的反编译功能一直调不出来 

师兄的可以在IDEA的里面点方法/类名进入其jar的实现,但是我的电脑调不出来。
经师兄查找可以了,一下为步骤:

IDEA --->settings 

在搜索框输入 Byte ,点击Plugins 把左边的JAVAByte点上,就可以通过ctrl + alt + 点击,进入jar的源码类的真正实现


2. 报错为:
[ERROR] 2018-01-09 00:35:13,140 method:org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:308)
Context initialization failed
org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.gemantic.dal.id.util.IdGenerator] for bean with name 'idGenerator' defined in URL [jar:file:/D:/huanjing/.m2/repository/com/gemantic/dal-dao/0.0.18/dal-dao-0.0.18.jar!/com/gemantic/dal/id/util/IdGenerator.class]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/hibernate/id/IdentifierGenerator
解决方案:
在core的pom配上:
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>3.3.2.GA</version>
    <exclusions>
        <exclusion>
            <artifactId>commons-collections</artifactId>
            <groupId>commons-collections</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.4.0.GA</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.4.0.GA</version>
</dependency>



收获:

1.  学习多线程

   多线程方法2:实现Runnable接口

public class MyRunnable implements Runnable {
private static int num = 0;
   private String name;
   public MyRunnable(String name){
num ++;
       this.name = name;
   }

@Override
   public void run(){
System.out.println("主动创建的第"+num+"个线程");
       System.out.println("子线程ID"+Thread.currentThread().getId());
   }
}
@Test
public void testTwo(){
System.out.println("子线程ID"+Thread.currentThread().getId());
   MyRunnable myRunnable1 = new MyRunnable("myRunnable1");
   Thread thread1 = new Thread(myRunnable1);
   thread1.start();
   MyRunnable myRunnable2 = new MyRunnable("myRunnable2");
   myRunnable2.run();
}

Thread类的实现源代码会发现Thread类是实现了Runnable接口的。

但是由于Java只允许单继承,所以如果自定义类需要继承其他类,则只能选择实现Runnable接口。


多线程方法三:使用ExecutorService、Callable、Future实现有返回结果的多线程

public void testThree() throws ExecutionException, InterruptedException{
System.out.println("----程序开始运行----");
   Long begin = System.currentTimeMillis();
   int taskSize = 5;
   // 创建一个线程池
   ExecutorService pool = Executors.newFixedThreadPool(taskSize);
   // 创建多个有返回值的任务
   List<Future> list = new ArrayList<Future>();
   for (int i = 0; i < taskSize; i++) {
Callable c = new MyCallable(i + " ");
       // 执行任务并获取Future对象
       Future f = pool.submit(c);
       // System.out.println(">>>" + f.get().toString());
       list.add(f);
   }
// 关闭线程池
   pool.shutdown();
   // 获取所有并发任务的运行结果
   for (Future f : list) {
// Future对象上获取任务的返回值,并输出到控制台
       System.out.println(">>>" + f.get().toString());
   }

Long end = System.currentTimeMillis();
   System.out.println("----程序结束运行----,程序运行时间【"
           + (end - begin) + "毫秒】");

}

总时间用了10016,而每个子线程花了1000ms以上



多线程关键在于上下文的切换:

对于线程的上下文切换实际上就是 存储和恢复CPU状态的过程,它使得线程执行能够从中断点恢复执行

线程上下文切换过程中会记录程序计数器、CPU寄存器状态等数据。

虽然多线程可以使得任务执行的效率得到提升,但是由于在线程切换时同样会带来一定的开销代价,并且多个线程会导致系统资源占用的增加,所以在进行多线程编程时要注意这些因素。




任务进度:文件接口 

 任务延期原因:环境配置和代码bug

 延期时间:2-9

DOME时间:2018-2-9



返回列表 返回列表
评论

    分享到