b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

多线程编程面试题_嵌入式linux多线程编程_多线程编程java

电脑杂谈  发布时间:2017-02-16 23:07:16  来源:网络整理

如果你即将去一家从事大型系统研发的公司进行Java面试,不可避免的会有多线程相关的问题。下面是一些针对初学者或者新手的问题,如果你已经具备良好的基础,那么你可以跳过本文,直接尝试针对进阶水平的Java多线程编程问题及解答。

解答:一个进程对应一个程序的执行,而一个线程则是进程执行过程中的一个单独的执行序列,一个进程可以包含多个线程。线程有时候也被称为轻量级进程.

一个Java虚拟机的实例运行在一个单独的进程中,不同的线程共享Java虚拟机进程所属的堆内存。这也是为什么不同的线程可以访问同一个对象。线程彼此共 享堆内存并保有他们自己独自的栈空间。这也是为什么当一个线程调用一个方法时,他的局部变量可以保证线程安全。但堆内存并不是线程安全的,必须通过显示的声明同步来确保线程安全。

解答:可以通过如下几种方式:

继承Thread类

实现Runnable接口

使用Executorframework(这会创建一个线程池)

  1. classCounter extendsThread { 
  2. //method where the thread execution will start  
  3. publicvoidrun(){ 
  4. //logic to execute in a thread     
  5. //let’s see how to start the threads 
  6. publicstaticvoidmain(String[] args){ 
  7. Thread t1 = newCounter(); 
  8. Thread t2 = newCounter(); 
  9. t1.start();  //start the first thread. This calls the run() method. 
  10. t2.start(); //this starts the 2nd thread. This calls the run() method.   
  1. classCounter extendsBase implementsRunnable{ 
  2. //method where the thread execution will start  
  3. publicvoidrun(){ 
  4. //logic to execute in a thread     
  5. //let us see how to start the threads 
  6. publicstaticvoidmain(String[] args){ 
  7. Thread t1 = newThread(newCounter()); 
  8. Thread t2 = newThread(newCounter()); 
  9. t1.start();  //start the first thread. This calls the run() method. 
  10. t2.start();  //this starts the 2nd thread. This calls the run() method.   


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-32846-1.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    • 周哀王姬去疾
      周哀王姬去疾

      你这不是自己在打自己的脸吗

    • 李浩楠
      李浩楠

      12海里指的是哪里

    热点图片
    拼命载入中...