What is thread in java and how its methods behave

1. What is Thread

As in oracle java specification, thread is one of two basic units of execution, the other is process. Process was present prior to thread, we can refer to one process as an application. When an application is running, the resources allocated to the applicaition cannot be shared among other applications. The process of application exection is clear, but not efficent. Assume a application that has a lot of calculate tasks, execute task one by one, that will take a lot of hours. It can not be accpetable.

In order to make application execution more effcient, thread is introduced, also called light-weight process. One process has at least one thread. As mentioned above, application with a lot of calculate tasks divides all tasks into many smaller tasks, one small task is bounded to one thread, these task threads are able to run concurrently.

We can take advantage of thread, but there are disadvantages meanwhile. Threads share the resource of application, it may occurs race condition. The bahaviors of threads in race condition, particularly when not correctly synchronized, can be confusing and counterintuitive.

2. Creation of Thread

There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass then should override the run method of class Thread. The Other way is to declare a class implements Runnable interface and implements the run method. An instance of that class then can be passed as as argument when creating an obejct of Thread.

3.Method in Thread

In this Section, We will focus on some important methods in Thread. These methods mainly fall into three types: Constructor method such as Thread(Runnable) and Thread(String), Static method such as yield() and sleep().and Instance method such as start() and join().

3.1 Constructor method

Constructor method is used to create a instance with keyword 'new', (except that declare a new class to be subclass of Thread supering any of Thread constructor methods in constructor method of subclass ). This part is easy-understanding, will not be discussed in this section.

3.2 Static method

Important static methods in Thread include sleep(long), sleep(long, int) and yield().

sleep(long) and sleep(long, int) cause the currently executing thread to sleep for specified number of milliseconds or plus nanoseconds, subject to the precision and accuracy of system timer and scheduler. This Thread does not lose the ownership of any monitors.

yield()  gives a hint to thread scheduler that the current thread is willing to yield its current use of processor. However, the hint is not mandatory, the scheduler is free to ignore this hint.

3.3 Static method

There are a lot of instance methods, for example: start(), join(long),  join(long, int), and interrupt()

Call start() to tell thread scheduler that this thread is ready to run , then the scheduler will schedule this thread to run in a proper time. It is never legal to start a thread more than once and a thread cannot restarted after it has completed execution.

join(long) and join(long, int)  suspend the current thread immediately until this thread dies or specified time is out,

interrupt() is to set to interrupt flag that is an indication to a thread to indicate that the thread should stop running. But it is up to programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate. For more informations . you can refer to this website https://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html.

 👉👉👉 自己搭建的租房网站:全网租房助手,m.kuairent.com,每天新增 500+房源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值