1 Creating threads using thread and runnable
"Experimental Purposes"
(1) To understand the use of the implementation of runnable interface methods to achieve multithreading.
(2) Master the setting of thread priority.
(3) Deepen the understanding of the thread state transition.
"Experimental Requirements"
Request one: The use of multithreading to achieve the rotation of the planet
Requirement two: Write a thread Syncdemo, design a method public class GetNumber () count from 1 to 20 stop. Among them, design two synchronization threads Athread and Bthread, all use GetNumber () to scramble these 20 numbers. Note the synchronous control of the thread athread and bthread to ensure that the count is stopped by 20.
Class Syncdemo implements Runnable {
static int num = 1;
Public synchronized Boolean GetNumber () throws Exception {
thread.sleep (MB);
if (num <=) {
System.out.println (Thread.CurrentThread (). GetName () + "" +num++);
return true;
}
return false;
}
public void Run () {a
try {while
(GetNumber ());
} catch (Exception e) {
//TODO auto-generated catch Blo CK
e.printstacktrace ();
}} public class Fd {
/**
* @param args */public
static void Main (string[] args) {
//TODO Auto-gene Rated method Stub
Syncdemo demo = new Syncdemo ();
Thread athread = new Thread (demo, "A");
Thread bthread = new Thread (demo, "B");
Athread.start ();
Bthread.start ();
}