Tags (separated by spaces): thread running state
Threads running state:
The following is a chart I prepared, we can serve as a reference:
1.New a thread subclass also created a thread;
after 2. Create a complete start () ----- Run,
3. then run from --- ---- sleep (time) ---- frozen ---- sleep (time) time to recover to run;
4. there is another, it's time threads running ------ wait time --- --- ------- then frozen state to notify () wake ----- then restored to operational status;
5. demise: the thread running / freeze: end stop () / run method ---- ----- demise;
6. the provisional state: the current state of awaiting execution of the CPU, with no executive power run eligibility,
7. freeze: give up the implementation of qualifications;
8. frozen ----- --- temporary recovery status / run state;
Gets an object and the name of the thread:
After the object is created, a name must match the name of the thread to be defined in the thread of things inside:
class Demo extends Thread{
private String name;
Demo(String name){
this.name=name;
}
public void run(){//将运行的代码放在run方法中
for(int x=0;x<60;x++) {
System.out.println(this.getName()+"demo run-----"+x);
}
}
}
class ThreadDemo{
public static void main(String[] args){
Demo d1= new Demo("one");//创建好一个线程
Demo d2= new Demo("two");
d1.start();
d2.start();
for(int x=0;x<60;x++){
System.out.println("helloworld!----"+x);
}
}
}
- The original thread has its own default name, Thread- number that scratch;
2. The above name is not not look good, where you can use the set method to define the name;
Gets the name of the current thread:
- static Thread currentThread (): Get the current thread;
getName (): Get the name of the thread;
set thread name: setName or constructor;
Exercise:
- Demand is a simple ticketing procedures, multiple windows selling tickets, selling No. 1 in the window when the No. 2 also sell; multiple windows at the same time selling tickets,
Observed following procedure:
class Ticket extends Thread{
private int tick=100;
public void run(){
while (true){
if(tick>0) {
System.out.println(Thread.currentThread().getName()+"sale:" + tick--);
}
}
}
}
class TickDemo{
public static void main(String[] args){
Ticket t1=new Ticket();
Ticket t2=new Ticket();
Ticket t3=new Ticket();
Ticket t4=new Ticket();
t1.start();
t2.start();
t3.start();
t4.start();
}
}
The results:
found 100 tickets sold out 400 tickets; each object which contains 100 tickets;
如何实现:100张票4个窗口正常售卖呢?,我们可以通过static定义变量来实现,如下
class Ticket extends Thread{
private static int tick=100;
public void run(){
while (true){
if(tick>0) {
System.out.println(Thread.currentThread().getName()+"sale:" + tick--);
}
}
}
}
class TickDemo{
public static void main(String[] args){
Ticket t1=new Ticket();
Ticket t2=new Ticket();
Ticket t3=new Ticket();
Ticket t4=new Ticket();
t1.start();
t2.start();
t3.start();
t4.start();
}
}
执行通过;
- 但是一般我们不使用静态,因为静态的生命周期比较长;
- 我们使用Thread创建线程不能实现了;
runable接口:
- Runable接口应该由那些打算通过某一线程执行实例的类来实现,类必须顶一个称为run的无参数方法,设计该接口的目的就是提供一个公共协议,例如,Thread类实现了Runable,激活的意思是某个线程已经启动并且尚未停止;
class Ticket implements Runnable{
private static int tick=100;
public void run(){
while (true){
if(tick>0) {
System.out.println(Thread.currentThread().getName()+"sale:" + tick--);
}
}
}
}
class TickDemo{
public static void main(String[] args){
// Ticket t1=new Ticket();
// Ticket t2=new Ticket();
// Ticket t3=new Ticket();
// Ticket t4=new Ticket();
// t1.start();
// t2.start();
// t3.start();
// t4.start();
Ticket t=new Ticket();
Thread t1=new Thread();
Thread t2=new Thread();
Thread t3=new Thread();
Thread t4=new Thread();
t1.start();
t2.start();
t3.start();
t4.start();
}
}
上述代码:能否实现呢?
分析:t1调用运行的run()是Thread里面的run,在这次代码里面未重写;
package com.wangaling;
class Ticket implements Runnable{
private static int tick=100;
public void run(){
while (true){
if(tick>0) {
System.out.println(Thread.currentThread().getName()+"sale:" + tick--);
}
}
}
}
class TickDemo{
public static void main(String[] args){
// Ticket t1=new Ticket();
// Ticket t2=new Ticket();
// Ticket t3=new Ticket();
// Ticket t4=new Ticket();
// t1.start();
// t2.start();
// t3.start();
// t4.start();
Ticket t=new Ticket();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
Thread t3=new Thread(t);
Thread t4=new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
执行结果:
上述代码可以正常实现售票的功能;
- 创建线程的第二种方式,实现runnable接口
步骤1:
1。定义类实现Runable接口;
2.覆盖Runable接口中的run方法:将线程要运行的代码存在在run方法中
3.通过Thread类建立线程对象
4.将Runable接口的子类对象作为实际参数传递给Thread类的构造函数:为什么要将Runable接口的子类的对象传递给Thread的构造函数,因为,自定义的run方法所属的对象是Runable接口的子类对象,所以要让线程去指定指定对象的run方法,就必须明确该run方法所属的对象;
5.调用Thread类的start方法开启线程,并调用Runable接口子类的run方法;
实现方式和继承方式有很么区别?
- 1.实现方式好处:避免了单继承的局限性,在定义线程的时候建议是用实现方式,可以避免单继承的实现性;
两种方式区别:
- 继承Thread:线程代码存放在Thread子类的run方法中;
实现Runable,线程代码存在接口的子类的run方法