<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FThread%2FCreate_Thread</id>
		<title>Java Tutorial/Thread/Create Thread - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FThread%2FCreate_Thread"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Thread/Create_Thread&amp;action=history"/>
		<updated>2026-04-22T01:28:30Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Thread/Create_Thread&amp;diff=2787&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Thread/Create_Thread&amp;diff=2787&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:26Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 17:44, 31 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Thread/Create_Thread&amp;diff=2788&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Thread/Create_Thread&amp;diff=2788&amp;oldid=prev"/>
				<updated>2010-05-31T15:18:09Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  Create a second thread. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
class NewThread implements Runnable {&lt;br /&gt;
  Thread t;&lt;br /&gt;
  NewThread() {&lt;br /&gt;
    t = new Thread(this, &amp;quot;Demo Thread&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;Child thread: &amp;quot; + t);&lt;br /&gt;
    t.start(); // Start the thread&lt;br /&gt;
  }&lt;br /&gt;
  public void run() {&lt;br /&gt;
    try {&lt;br /&gt;
      for (int i = 5; i &amp;gt; 0; i--) {&lt;br /&gt;
        System.out.println(&amp;quot;Child Thread: &amp;quot; + i);&lt;br /&gt;
        Thread.sleep(500);&lt;br /&gt;
      }&lt;br /&gt;
    } catch (InterruptedException e) {&lt;br /&gt;
      System.out.println(&amp;quot;Child interrupted.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;Exiting child thread.&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class ThreadDemo {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    new NewThread();&lt;br /&gt;
    try {&lt;br /&gt;
      for (int i = 5; i &amp;gt; 0; i--) {&lt;br /&gt;
        System.out.println(&amp;quot;Main Thread: &amp;quot; + i);&lt;br /&gt;
        Thread.sleep(1000);&lt;br /&gt;
      }&lt;br /&gt;
    } catch (InterruptedException e) {&lt;br /&gt;
      System.out.println(&amp;quot;Main thread interrupted.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;Main thread exiting.&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Create a second thread by extending Thread ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
class NewThread extends Thread {&lt;br /&gt;
  NewThread() {&lt;br /&gt;
    super(&amp;quot;Demo Thread&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;Child thread: &amp;quot; + this);&lt;br /&gt;
    start(); // Start the thread&lt;br /&gt;
  }&lt;br /&gt;
  public void run() {&lt;br /&gt;
    try {&lt;br /&gt;
      for (int i = 5; i &amp;gt; 0; i--) {&lt;br /&gt;
        System.out.println(&amp;quot;Child Thread: &amp;quot; + i);&lt;br /&gt;
        Thread.sleep(500);&lt;br /&gt;
      }&lt;br /&gt;
    } catch (InterruptedException e) {&lt;br /&gt;
      System.out.println(&amp;quot;Child interrupted.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;Exiting child thread.&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class ExtendThread {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    new NewThread(); // create a new thread&lt;br /&gt;
    try {&lt;br /&gt;
      for (int i = 5; i &amp;gt; 0; i--) {&lt;br /&gt;
        System.out.println(&amp;quot;Main Thread: &amp;quot; + i);&lt;br /&gt;
        Thread.sleep(1000);&lt;br /&gt;
      }&lt;br /&gt;
    } catch (InterruptedException e) {&lt;br /&gt;
      System.out.println(&amp;quot;Main thread interrupted.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;Main thread exiting.&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Create multiple threads. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
class NewThread implements Runnable {&lt;br /&gt;
  String name; // name of thread&lt;br /&gt;
  Thread t;&lt;br /&gt;
  NewThread(String threadname) {&lt;br /&gt;
    name = threadname;&lt;br /&gt;
    t = new Thread(this, name);&lt;br /&gt;
    System.out.println(&amp;quot;New thread: &amp;quot; + t);&lt;br /&gt;
    t.start(); // Start the thread&lt;br /&gt;
  }&lt;br /&gt;
  public void run() {&lt;br /&gt;
    try {&lt;br /&gt;
      for (int i = 5; i &amp;gt; 0; i--) {&lt;br /&gt;
        System.out.println(name + &amp;quot;: &amp;quot; + i);&lt;br /&gt;
        Thread.sleep(1000);&lt;br /&gt;
      }&lt;br /&gt;
    } catch (InterruptedException e) {&lt;br /&gt;
      System.out.println(name + &amp;quot;Interrupted&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(name + &amp;quot; exiting.&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class MultiThreadDemo {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    new NewThread(&amp;quot;One&amp;quot;); // start threads&lt;br /&gt;
    new NewThread(&amp;quot;Two&amp;quot;);&lt;br /&gt;
    new NewThread(&amp;quot;Three&amp;quot;);&lt;br /&gt;
    try {&lt;br /&gt;
      Thread.sleep(10000);&lt;br /&gt;
    } catch (InterruptedException e) {&lt;br /&gt;
      System.out.println(&amp;quot;Main thread Interrupted&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;Main thread exiting.&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Creating a Thread ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;A thread is a basic processing unit to which an operating system allocates processor time, and more than one thread can be executing code inside a process. &lt;br /&gt;
(Java 5: A Beginner&amp;quot;s Tutorial by Budi Kurniawan Brainy Software Corp. 2006&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Every Java program has at least one thread, the thread that executes the Java program. &lt;br /&gt;
It is created when you invoke the static main method of your Java class.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;There are two ways to create a thread.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;OL&amp;gt;&amp;lt;LI&amp;gt;Extend the java.lang.Thread class&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;Implement the java.lang.Runnable interface.&amp;lt;/LI&amp;gt;&amp;lt;/OL&amp;gt;&lt;br /&gt;
&amp;lt;OL&amp;gt;&amp;lt;LI&amp;gt;Once you have a Thread object, you call its start method to start the thread.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;When a thread is started, its run method is executed.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;Once the run method returns or throws an exception, the thread dies and will be garbage-collected.&amp;lt;/LI&amp;gt;&amp;lt;/OL&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Every Thread has a state and a Thread can be in one of these six states.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;OL&amp;gt;&amp;lt;LI&amp;gt;new. A state in which a thread has not been started.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;runnable. A state in which a thread is executing.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;blocked. A state in which a thread is waiting for a lock to access an object.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;waiting. A state in which a thread is waiting indefinitely for another thread to perform an action.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;timed__waiting. A state in which a thread is waiting for up to a specified period of time for another thread to perform an action.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;terminated. A state in which a thread has exited.&amp;lt;/LI&amp;gt;&amp;lt;/OL&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;The values that represent these states are encapsulated in the java.lang.Thread.State enum. &lt;br /&gt;
The members of this enum are NEW, RUNNABLE, BLOCKED, WAITING, TIMED__WAITING, and TERMINATED.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Creating Thread: Deriving a Subclass of Thread ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
class TryThread extends Thread {&lt;br /&gt;
  public TryThread(String firstName, String secondName, long delay) {&lt;br /&gt;
    this.firstName = firstName;&lt;br /&gt;
    this.secondName = secondName;&lt;br /&gt;
    aWhile = delay;&lt;br /&gt;
    setDaemon(true);&lt;br /&gt;
  }&lt;br /&gt;
  public void run() {&lt;br /&gt;
    try {&lt;br /&gt;
      while (true) {&lt;br /&gt;
        System.out.print(firstName);&lt;br /&gt;
        sleep(aWhile);&lt;br /&gt;
        System.out.print(secondName + &amp;quot;\n&amp;quot;);&lt;br /&gt;
      }&lt;br /&gt;
    } catch (InterruptedException e) {&lt;br /&gt;
      System.out.println(firstName + secondName + e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  private String firstName;&lt;br /&gt;
  private String secondName;&lt;br /&gt;
  private long aWhile;&lt;br /&gt;
}&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    Thread first = new TryThread(&amp;quot;A &amp;quot;, &amp;quot;a  &amp;quot;, 200L);&lt;br /&gt;
    Thread second = new TryThread(&amp;quot;B &amp;quot;, &amp;quot;b &amp;quot;, 300L);&lt;br /&gt;
    Thread third = new TryThread(&amp;quot;C &amp;quot;, &amp;quot;c &amp;quot;, 500L);&lt;br /&gt;
    System.out.println(&amp;quot;Press Enter when you have had enough...\n&amp;quot;);&lt;br /&gt;
    first.start();&lt;br /&gt;
    second.start();&lt;br /&gt;
    third.start();&lt;br /&gt;
    try {&lt;br /&gt;
      System.in.read();&lt;br /&gt;
      System.out.println(&amp;quot;Enter pressed...\n&amp;quot;);&lt;br /&gt;
    } catch (IOException e) {&lt;br /&gt;
      System.out.println(e);&lt;br /&gt;
    }&lt;br /&gt;
    return;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Creating Thread Objects: Implementing the run() Method in Runnable interface ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
class TryThread implements Runnable {&lt;br /&gt;
  public TryThread(String firstName, String secondName, long delay) {&lt;br /&gt;
    this.firstName = firstName;&lt;br /&gt;
    this.secondName = secondName;&lt;br /&gt;
    aWhile = delay;&lt;br /&gt;
  }&lt;br /&gt;
  public void run() {&lt;br /&gt;
    try {&lt;br /&gt;
      while (true) {&lt;br /&gt;
        System.out.print(firstName);&lt;br /&gt;
        Thread.sleep(aWhile);&lt;br /&gt;
        System.out.print(secondName + &amp;quot;\n&amp;quot;);&lt;br /&gt;
      }&lt;br /&gt;
    } catch (InterruptedException e) {&lt;br /&gt;
      System.out.println(firstName + secondName + e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  private String firstName;&lt;br /&gt;
  private String secondName;&lt;br /&gt;
  private long aWhile;&lt;br /&gt;
}&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    Thread first = new Thread(new TryThread(&amp;quot;A &amp;quot;, &amp;quot;a &amp;quot;, 200L));&lt;br /&gt;
    Thread second = new Thread(new TryThread(&amp;quot;B &amp;quot;, &amp;quot;b &amp;quot;, 300L));&lt;br /&gt;
    Thread third = new Thread(new TryThread(&amp;quot;C &amp;quot;, &amp;quot;c &amp;quot;, 500L));&lt;br /&gt;
    System.out.println(&amp;quot;Press Enter when you have had enough...\n&amp;quot;);&lt;br /&gt;
    first.start();&lt;br /&gt;
    second.start();&lt;br /&gt;
    third.start();&lt;br /&gt;
    try {&lt;br /&gt;
      System.in.read();&lt;br /&gt;
      System.out.println(&amp;quot;Enter pressed...\n&amp;quot;);&lt;br /&gt;
    } catch (IOException e) {&lt;br /&gt;
      System.out.println(e);&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;Ending main()&amp;quot;);&lt;br /&gt;
    return;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>