Thread:classMyThreadextendsthread{Private intticketscont=5;//There are 5 train tickets altogether . PrivateString name;//window, which is also the name of the thread PublicMyThread (String name) { This. name=name; } @Override Public voidrun () { while(ticketscont>0) {Ticketscont--;//If there's a ticket, sell a ticket.System.out.println (name+ "sold 1 tickets, the remaining votes:" +Ticketscont); } }} Public classticketsthread{ Public Static
implementation of the Runnable interface has the following advantages over the inheritance thread class: Avoids the limitations of Java's single inheritance features , enhances the robustness of programs, allows code to be shared by multiple threads, and code is independent of data, and a thread area that is suitable for multiple identical program code handles the same resource. Example of selling tickets
Inheritance Thread Implementation
Class Mythread extends thread{
private i
, Runnable code can be shared by multiple threads (Thread instances), which is suitable for processing unified resources by multiple threads.
Example: Simulate ticket sales. Assume that there are five tickets left, two methods are used to create thread simulation respectively.
TicketThread. java // use inheritance to simulate the sale of five tickets in three windows
1 public class TicketThread {2 public static void main (String [] args) {3 // three threads are created, simulate three windows to
following code is used to implement this example.
Defining the Order Interface
/***/Publicinterface Order {// Sell Coffee void sell ();}
Specific order Fulfillment
Public class Implements Order { // coffee taste public String flavor; Public Coffeeorder (String flavor) { this. Flavor= flavor; } @Override publicvoid sell () { System.out.println ("sold a copy" +flavor + "The coffee. "); }}
Order Factory C
the start () method with the thread class or its child class object.Why use the start () method instead of the run () method to start a thread? because the start () method produces the system resources required to run the thread, schedules it to run, and calls the line Cheng (run () method).A thread can only be started once, no matter how many times the start () method is called, the result is only one thread.2. Thread-sharing resourcesIt is recommended to implement the Runnable interface with
Take advantage of state mode before our code:
public class Gumballmachine {final static int sold_out = 0;
Final static int no_quarter = 1;
Final static int has_quarter = 2;
Final static int sold = 3;
int state = Sold_out;
int count = 0;
public gumballmachine (int count) {This.count = count;
if (Count > 0) {state = No_quarter; }//Input money public void Insertquarter () {if (state = Has_quarter) {System.out.println ("You can ' t insert ano
Qq Hyun dance introduced new theme box update in November 15! Let's take a look at what you like! The audio box will also be updated for everyone. In this list of treasures, in addition to powder bath towels, Rainbow waves, Thunderball float clouds and other classic non-sold products, there are also many new products not sold in the November version. Let's take a look! 1 bang float cloud, powder bath towel,
The following is an example of a multi-thread ticket buying error:
Public class SaleTicketMutiThread implements Runnable {Int tickets = 100;Int temp = tickets;Boolean flag = true;@ OverridePublic void run (){While (flag ){If (tickets> 0 ){Try {Thread. sleep (30 );} Catch (InterruptedException e ){E. printStackTrace ();}Sale ();} Else {Flag = false;System. out. println (Thread. currentThread (). getName () + "sold out ");}}}Public synchronized void sal
Use the status mode before our code:
public class Gumballmachine {final static int sold_out = 0;
Final static int no_quarter = 1;
Final static int has_quarter = 2;
Final static int SOLD = 3;
int state = Sold_out;
int count = 0;
public gumballmachine (int count) {This.count = count;
if (Count > 0) {state = No_quarter; }}//Put money public void Insertquarter () {if (state = = Has_quarter) {System.out.println ("You can ' t insert another
Q
thread.The Code is as follows:
Class mythread2 implements runnable {public void run () {// thread body for (INT I = 0; I
Now we use a classic case to illustrate the differences between the two methods:
Ticket sales example. This example is basically available in every Java getting started book!
Requirement: If you have 50 tickets, you need to sell them in three ticket sales windows. You can use two ways to enable the thread to buy tickets. What are the differences between the two methods?
1.
On the live session, Android 4.3 is not as sexy as the Nexus 7 and not as attractive as a Chromecast TV stick, but it has a lot of tough new features, and here's a look at top 5.1. Permissions ConfigurationSupports multiple account logins on an end device, but with content restrictions (Restricted Profiles), such as content permission settings, can prevent your child from downloading a paid game or app from Google Play.2. OpenGL ES 3.0The new OpenGL E
You may also have the experience of opening an advertisement after opening a website. If it is in a crowded environment, this accident can be very embarrassing. Google's latest Chrome 32 Beta browser can help you quickly turn off the sound of automatic playback.
In Chrome 32 Beta, tabs not only display the webpage name, but also display different icons on the rightmost side to indicate the different states of the webpage, for example, the horn icon indicates that the webpage is playing audio, t
Sony announced that the PlayStation Vue will start landing next week on Android devices, the PlayStation Vue was launched in March 2015 and is Sony's cloud-based DVR live TV service, Android users will then be able to access on-demand content via a smartphone or tablet, watch a TV show online (the base package includes the 55+ channel), and the My shows feature, and the app also supports Chromecast or any Google cast device.In March, Sony reduced its
Sony announced that the PlayStation Vue will start landing next week on Android devices, the PlayStation Vue was launched in March 2015 and is Sony's cloud-based DVR live TV service, Android users will then be able to access on-demand content via a smartphone or tablet, watch a TV show online (the base package includes the 55+ channel), and the My shows feature, and the app also supports Chromecast or any Google cast device.In March, Sony reduced its
Is Android going into the television business? What is the Android TV? What is the role of the following to the small series to introduce you.
Google is ready to go through the Android TV-led bedroom gaming market, the device is an Amazon-like Fire TV set-top box, according to internal documents obtained by The Verge.
According to the document, the Android TV is an "entertainment interface" for discovering "fun, mobile, and fast" entertainment content, including movies, TV and games. Google ad
Introduction to Mobile WebMobile Product ListTV-based devices, including: Smart TVs, game consoles (such as Xbox) and set-top boxes (such as Chromecast, Amazon Fire TV, etc.)Desktop devices, including operating systems using laptops, such as Windows,mac OS X, Linux or Chrome OS? tablet? phablets, phone with a screen greater than 5.5Smart phones, using a large operating system such as Android,ios, Windows or BlackBerry phones.? Social devices: Firefox
features will be more and more attentionLarge manufacturers more pragmatic, more compliance with the platform design norms, only in the case of the need to re-invent the wheel, because of the implementation, adaptation, user learning costs and other aspects of consideration, self-created interactive effect may not be ideal.(IOS App, ideally should fit IPhone 5/6/6+ resolution, Ipad/ipad Pro's 25%, 50%, 75%, 100% wide resolution)650) this.width=650; "Src=" Http://image.uisdc.com/wp-content/uploa
about Web Alarms API , please refer to the relevant details documentation.
Presentation API
Presentation API The goal is to have the projector or TV such a second display device can be Web use, including all through Wired ( HDMI , DVI and other) connections and via Wireless ( MiraCast, Chromecast, DLNA, AirPlay , etc.) of the device. What this API does is to implement message interoperability between the request page and the presentation page on the
should fit the resolution of the IPhone 5/6/6+, Ipad/ipad Pro 25%, 50%, 75%, 100% wide)
Support Platform features (3D touch, Live Photos, Chromecast, split screen) in Early adopter to establish a reputation, get free spread point, but also the middle of the user from competing products in the hands of the draw over.
But the 3D touch is still a gimmick.
3. Dynamic use is more common, but more in micro-interaction
Too gorgeous animatio
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.