Creating threads in Java in the first few ways

Source: Internet
Author: User

November 19, 2015 10:16:33

Data analysis player

Create a thread in the first way: each thread is dealing with a different resource
Each thread corresponds to 20 movie tickets, and there is no relationship between them, which means that each thread is equal and has no precedence
Class Tc extends Thread
{
private int tickets = 20;//Each thread sells 20 tickets each

public String name;
Public Tc (String name)
{
THIS.name = name;
}
public void Run ()
{
while (Tickets > 0)
{
System.out.printf ("%s window is selling%d tickets \ n", name,tickets);
Tickets--;
}
}
}
public class Zhang3
{
public static void Main (string[] args)
{
TC T1 = new TC ("Old person window----");
T1.run ();//implement a window to sell tickets

TC T2 = new TC ("-------Children's Window");
T2.run ();//Different class objects whose properties occupy a different memory space, which is fundamental

TC T3 = new TC ("---Middle-aged window---");
T3.run ();
}
}
/*
Operation Result:
Old Man window----window is selling 20 tickets
Old Man window----window is selling 19 tickets
Old Man window----window is selling 18 tickets
Old Man window----window is selling 17 tickets
Old Man window----window is selling 16 tickets
Old Man window----window is selling 15 tickets
Old Man window----window is selling 14 tickets
Old Man window----window is selling 13 tickets
Old Man window----window is selling 12 tickets
Old Man window----window is selling 11 tickets
Old Man window----window is selling 10 tickets
Old Man window----window is selling 9 tickets
Old Man window----window is selling 8 tickets
Old Man window----window is selling 7 tickets
Old Man window----window is selling 6 tickets
Old Man window----window is selling 5 tickets
Old Man window----window is selling 4 tickets
Old Man window----window is selling 3 tickets
Old Man window----window is selling 2 tickets
Old Man window----window is selling 1 tickets
-------Children Window window is selling 20 tickets
-------Children Window window is selling 19 tickets
-------Children window window is selling 18 tickets
-------Children window window is selling 17 tickets
-------Children Window window is selling 16 tickets
-------Children window window is selling 15 tickets
-------Children window window is selling 14 tickets
-------Children Window window is selling 13 tickets
-------Children Window window is selling 12 tickets
-------Children Window window is selling 11 tickets
-------Children window window is selling 10 tickets
-------Children window window is selling 9 tickets
-------Children Window window is selling 8 tickets
-------Children Window window is selling 7 tickets
-------Children Window window is selling 6 tickets
-------Children Window window is selling 5 tickets
-------Children Window window is selling 4 tickets
-------Children window window is selling 3 tickets
-------Children Window window is selling 2 tickets
-------Children Window window is selling 1 tickets
---Middle-aged window---window is selling 20 tickets
---Middle-aged window---window is selling 19 tickets
---Middle-aged window---window is selling 18 tickets
---Middle-aged window---window is selling 17 tickets
---Middle-aged window---window is selling 16 tickets
---Middle-aged window---window is selling 15 tickets
---Middle-aged window---window is selling 14 tickets
---Middle-aged window---window is selling 13 tickets
---Middle-aged window---window is selling 12 tickets
---Middle-aged window---window is selling 11 tickets
---Middle-aged window---window is selling 10 tickets
---Middle-aged window---window is selling 9 tickets
---Middle-aged window---window is selling 8 tickets
---Middle-aged window---window is selling 7 tickets
---Middle-aged window---window is selling 6 tickets
---Middle-aged window---window is selling 5 tickets
---Middle-aged window---window is selling 4 tickets
---Middle-aged window---window is selling 3 tickets
---Middle-aged window---window is selling 2 tickets
---Middle-aged window---window is selling 1 tickets
*/
Create the thread in the second way: same as each thread can handle different resources
Class Tc implements Runnable
{
private int tickets = 20;//Each thread sells 20 tickets each

public String name;
Public Tc (String name)
{
THIS.name = name;
}
public void Run ()
{
while (Tickets > 0)
{
System.out.printf ("%s window is selling%d tickets \ n", name,tickets);
Tickets--;
}
}
}
public class Zhang4
{
public static void Main (string[] args)
{
TC AA = new TC ("Old person window----");
thread T1 = new thread (AA);
T1.run ();//implement a window to sell tickets

TC BB = new TC ("-------Children's Window");
Thread t2 = new Thread (BB);
T2.run ();//Different class objects whose properties occupy a different memory space, which is fundamental

TC CC = new TC ("---Middle-aged window---");
thread t3 = new Thread (CC);
T3.run ();
}
}
/*
Run result: Same as above program
Old Man window----window is selling 20 tickets
Old Man window----window is selling 19 tickets
Old Man window----window is selling 18 tickets
Old Man window----window is selling 17 tickets
Old Man window----window is selling 16 tickets
Old Man window----window is selling 15 tickets
Old Man window----window is selling 14 tickets
Old Man window----window is selling 13 tickets
Old Man window----window is selling 12 tickets
Old Man window----window is selling 11 tickets
Old Man window----window is selling 10 tickets
Old Man window----window is selling 9 tickets
Old Man window----window is selling 8 tickets
Old Man window----window is selling 7 tickets
Old Man window----window is selling 6 tickets
Old Man window----window is selling 5 tickets
Old Man window----window is selling 4 tickets
Old Man window----window is selling 3 tickets
Old Man window----window is selling 2 tickets
Old Man window----window is selling 1 tickets
-------Children Window window is selling 20 sheets
-------Children Window window is selling 19 sheets
-------Children window window is selling 18 sheets
-------Children window window is selling 17 sheets
-------Children Window window is selling 16 sheets
-------Children window window is selling 15 sheets
-------Children window window is selling 14 sheets
-------Children Window window is selling 13 sheets
-------Children Window window is selling 12 sheets
-------Children Window window is selling 11 sheets
-------Children window window is selling 10 sheets
-------Children window window is selling 9 tickets
-------Children Window window is selling 8 tickets
-------Children Window window is selling 7 tickets
-------Children Window window is selling 6 tickets
-------Children Window window is selling 5 tickets
-------Children Window window is selling 4 tickets
-------Children window window is selling 3 tickets
-------Children Window window is selling 2 tickets
-------Children Window window is selling 1 tickets
---Middle-aged window---window is selling 20 tickets
---Middle-aged window---window is selling 19 tickets
---Middle-aged window---window is selling 18 tickets
---Middle-aged window---window is selling 17 tickets
---Middle-aged window---window is selling 16 tickets
---Middle-aged window---window is selling 15 tickets
---Middle-aged window---window is selling 14 tickets
---Middle-aged window---window is selling 13 tickets
---Middle-aged window---window is selling 12 tickets
---Middle-aged window---window is selling 11 tickets
---Middle-aged window---window is selling 10 tickets
---Middle-aged window---window is selling 9 tickets
---Middle-aged window---window is selling 8 tickets
---Middle-aged window---window is selling 7 tickets
---Middle-aged window---window is selling 6 tickets
---Middle-aged window---window is selling 5 tickets
---Middle-aged window---window is selling 4 tickets
---Middle-aged window---window is selling 3 tickets
---Middle-aged window---window is selling 2 tickets
---Middle-aged window---window is selling 1 tickets
*/
Create a thread in the second way: multiple threads are working on the same resource
Class Tc implements Runnable
{
private int tickets = 10000;//3 threads handle 100 tickets altogether

public void Run ()
{
while (true)
{
Synchronized ("Zhang")
{
if (Tickets > 0)
{
System.out.printf ("%s window is selling%d tickets \ n", Thread.CurrentThread (). GetName (), tickets);
Tickets--;

}
Else
Break
}
}
}
}
public class Zhang5
{
public static void Main (string[] args)
{
TC AA = new TC ();
thread T1 = new thread (AA); AA Object
T1.setname ("Old person window");
T1.start ();//implement a window to sell tickets

Thread t2 = new Thread (AA); AA Object
T2.setname ("Children's Window");
T2.start ();//Different class objects whose properties occupy a different memory space, which is fundamental

thread t3 = new thread (AA); AA Object
T3.setname ("Middle-aged window");
T3.start ();
}
}
/*
The result is: three ticket offices to handle 1000 tickets together
*/
/* Integrated: The implementation of the Runnable interface has unparalleled advantages over the extended thread class.
This approach is not only beneficial to the robustness of the program, so that the code can be shared by multiple threads, and the code and data resources are relatively independent,
This makes it particularly suitable for multiple threads with the same code to handle the same resource situation.
In this way, the thread, code and data resources are effectively separated, which embodies the idea of object-oriented programming.
As a result, almost all multithreaded programs are done by implementing the Runnable interface. */

Creating threads in Java in the first few ways

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.