Java Multithreading

ref: http://www.studytonight.com/java/multithreading-in-java

Multithreading

A program can be divided into a number of small processes. Each small process can be addressed as a single thread (a lightweight process). Multithreaded programs contain two or more threads that can run concurrently. This means that a single program can perform two or more tasks simultaneously. For example, one thread is writing content on a file at the same time another thread is performing spelling check.

In Java, thread means two different things:

1. an instance of Thread

2. a thread execution

An instance of Thread class is just an object, like any other object in java. But a thread of execution means an individual "lightweight" process that has its own call stack. In java each thread has its own call stack

The main thread

Even if you don‘t create any thread in your program, a thread called main thread is still created. Although the main thread is automatically created, you can control it by obtaining a reference to it by calling currentThread() method.

Two important things to know about main thread are,

  • It is the thread from which other threads will be produced.
  • main thread must be always the last thread to finish execution.
class MainThread
{
 public static void main(String[] args)
 {
  Thread t=Thread.currentThread();
  t.setName("MainThread");
  System.out.println("Name of thread is "+t);
 }
}
Output :
Name of thread is Thread[MainThread,5,main]

Life cycle of a Thread

  1. New : A thread begins its life cycle in the new state. It remains in this state until the start() method is called on it.
  2. Runable : After invocation of start() method on new thread, the thread becomes runable.
  3. Running : A method is in running thread if the thread scheduler has selected it.
  4. Waiting : A thread is waiting for another thread to perform a task. In this stage the thread is still alive.
  5. Terminated : A thread enter the terminated state when it complete its task

Thread Priorities

Every thread has a priority that helps the operating system determine the order in which threads are scheduled for execution. In java thread priority ranges between,

  • MIN-PRIORITY (a constant of 1)
  • MAX-PRIORITY (a constant of 10)

By default every thread is given a NORM-PRIORITY(5). The main thread always have NORM-PRIORITY.


				
时间: 2024-08-27 08:10:18

Java Multithreading的相关文章

Java Multithreading Interview Questions

Java Multithreading Interview Questions Multithreading and Synchronization is considered as the typical chapter in java programming. In game development company, mulithreading related interview questions are asked mostly. A list of frequently asked j

Java 多线程 (转)

http://www.ibm.com/developerworks/cn/java/j-thread/index.html http://www.ibm.com/developerworks/cn/java/multithreading/index.html http://www.ibm.com/developerworks/cn/education/java/j-threads/j-threads.html 线程的概念基本清楚, 关键是访问共享变量时, 一定要注意. volatile: 对于简

java多线程编码注意事项

Sole purpose of using concurrency is to produce scalable and faster program. But always remember, speed comes after correctness. Your Java program must follow its invariant in all conditions, which it would, if executed in sequential manner. If you a

java程序中的多线程(转)

为什么会排队等待? 下面的这个简单的 Java 程序完成四项不相关的任务.这样的程序有单个控制线程,控制在这四个任务之间线性地移动.此外,因为所需的资源 ― 打印机.磁盘.数据库和显示屏 -- 由于硬件和软件的限制都有内在的潜伏时间,所以每项任务都包含明显的等待时间.因此,程序在访问数据库之前必须等待打印机完成打印文件的任务,等等.如果您正在等待程序的完成,则这是对计算资源和您的时间的一种拙劣使用.改进此程序的一种方法是使它成为多线程的. 四项不相关的任务 class myclass { sta

Top 10 tough core Java interview questions answers programming

Tough core Java interview questions and answersWhat is tough core java interview question ? Why do people look for tough Java questions before going for interview? well I don't thing I need to answer these tough questions because its pretty natural t

What skills you need to become a full stack java developer?

For a full stack Java developer you should start with learning backend and front-end technologies From the backend perspective: Java, multithreading, collections, jdbc, etc. Spring framework Hibernate Get good hold of SQL. You can use mysql. Learn th

线程安全的生产者消费者四种实现方法

问题描述 在IT技术面试过程中,我们经常会遇到生产者消费者问题(Producer-consumer problem), 这是多线程并发协作问题的经典案例.场景中包含三个对象,生产者(Producer),消费者(Consumer)以及一个固定大小的缓冲区(Buffer).生产者的主要作用是不断生成数据放到缓冲区,消费者则从缓冲区不断消耗数据.该问题的关键是如何线程安全的操作共享数据块,保证生产者线程和消费者线程可以正确的更新数据块,主要考虑 1. 生产者不会在缓冲区满时加入数据. 2. 消费者应当

[Java Basics] multi-threading

1, Interrupt Interruption in Java is not pre-emptive. Put another way both threads have to cooperate in order to process the interrupt properly. If the target thread does not poll the interrupted status the interrupt is effectively ignored. Polling o

Java简明教程 12.多线程(multithreading)

单线程和多线程 关于它们的区别,zhihu上有一个回答,我认为十分不错,如下: 1. 单进程单线程:一个人在一个桌子上吃菜. 2. 单进程多线程:多个人在同一个桌子上一起吃菜. 3. 多进程单线程:多个人每个人在自己的桌子上吃菜. 多线程的问题是多个人同时吃一道菜的时候容易发生争抢.例如两个人同时夹一个菜,一个人刚伸出筷子,结果伸到的时候已经被夹走菜了.此时就必须等一个人夹一口之后,在还给另外一个人夹菜,也就是说资源共享就会发生冲突争抢. 例子: 多线程: 浏览器浏览一个页面,里面有很多图片,多