First describe the state of the thread. The state transition of a thread is the basis of thread control. The total thread state can be divided into five states: life, Death, can run, run, wait/block;
650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/9A/CE/wKiom1la-XfzTou1AAEW73xPJpo922.png-wh_500x0-wm_ 3-wmp_4-s_3263872507.png "title=" thread state diagram. png "alt=" wkiom1la-xfztou1aaew73xpjpo922.png-wh_50 "/>
New state: The thread object has been created, and the start () method has not been invoked on it.
2. Operational status: When the thread is eligible to run, but the scheduler has not selected it as the state in which the thread is running. When the start () method is called, the thread first enters the operational state. After the thread has run or has returned from blocking, waiting, or sleeping, it returns to the operational state.
3. Running state: The thread Scheduler selects a thread from a running pool as the current thread is in the state of the thread. This is also the only way that a thread goes into a running state.
4. Wait/block/Sleep state: This is the state at which the thread is eligible to run. In fact, this three-state combination is one, and its common denominator is that the thread is still alive, but there is no condition to run at the moment. In other words, it is operational, but if an event occurs, he may return to the operational state.
5. Death state: When the thread's run () method finishes, it is considered dead. This thread object may be alive, but it is not a separate thread. Once a thread dies, it cannot be resurrected. If you call the start () method on a dead thread, the java.lang.IllegalThreadStateException exception is thrown.
In Scala, there is no concept of threading. Use actor instead of thread.
In the 2.10.x version, the actor class has been deprecated.
Code case
Import scala.actors.actor/** * desc: scala Actor uses *///sub-thread 1class myactor1 extends actor { val ty = "MyActor1" override def act (): unit = { for (i <- 1 to &NBSP;100) { println (S "i= $i") println (S "myactor1_thread_id:---${thread.currentthread (). getId}") println (s "Myactor1_thread_name:---${thread.currentthread (). GetName}") thread.sleep ( ) } }}//Child thread 2class myactor2 extends actor { val ty = "MyActor2" override def act (): unit = { for (i <- 1 to 100) { println (S "i= $i") &nbsP; println (S "myactor2_thread_id:---${thread.currentthread (). getId}") println (S "Myactor2_thread_name:---${thread.currentthread (). GetName}") thread.sleep ( } }}object Demo01MyActor { ) def fortest (): unit = { for (i <- 1 to &NBSP;50) { println (S "fortest: $i") Thread.Sleep ( } } //) main thread def main (args: Array[string]) { val actor1 = new myactor1 //new actor actor1.start () //actor into Ready state demo01myactor.fortest () The main thread calls a loop (simulates the main thread into a blocked state) val actor2 = new myactor2 //new actor acTor2.start () //actor enters the ready state println (S "main_thread_id:---${thread.currentthread (). GETID} ") println (S" Main_thread_name:---${thread.currentthread (). GetName} ") } }
Sub-thread: myactor
Class Myactor extends Actor {
Override Def Act (): Unit = {
while (true) {
Receive ({
Case "starting" + = {
println ("Actor is start-to-do anything!")
You can write a business logic operation for a child thread in this place.
}
Case "stoping" + = {
println ("Actor is stop-to-do anything!")
When a child thread wants to end, the part of the operation
}
})
}
}
}
Object Demo02receiveactor {
def main (args:array[string]) {
Val actor = new Myactor
Actor.start ()
println ("Myactor is Start")
Actor! "Starting"//Send asynchronous message, no return value
Actor! "Stoping"//Send asynchronous message, no return value
To send data to a child thread in a loop
for (I <-1 to 5) actor! "Starting"
}
}
This article is from the "Star Moon Love" blog, please be sure to keep this source http://xuegodxingyue.blog.51cto.com/5989753/1944344
Scala multi-thread actor concurrent programming model