Introduction to Java Concurrency program _java

Source: Internet
Author: User

Today I looked at the Java concurrency Program, wrote a starter program, and set the thread priority.

Class Elem implements runnable{public
  static int id = 0;
  private int cutdown = 5;
  private int priority;
  
  public void setpriority (int priority) {
    this.priority = priority;
  }
  
  public int getpriority () {return
    this.priority;
  }
  public void Run () {
    thread.currentthread (). SetPriority (priority);
    int threadId = id++;
    while (cutdown--> 0) {
      double d = 1.2;
      while (D < 10000)
        D = d + (MATH.E + Math.PI)/n;
      System.out.println ("#" + ThreadId + "(" + Cutdown + ")");
}} public class Basic {public
  static void Main (String args[]) {for
    (int i = 0; i < i++) {
      Elem e = new E Lem ();
      if (i = = 0)
        e.setpriority (thread.max_priority);
      else
        e.setpriority (thread.min_priority);
      Thread t = new thread (e);
      T.start ();}}

Because the machine is very strong, so start and do not see the effect of concurrency, the feeling is performed sequentially, so in the middle of the addition of floating-point arithmetic to delay time.

Of course, the main function can use executors to manage threads.

Import java.util.concurrent.*;
  Class Elem implements runnable{public static int id = 0;
  private int cutdown = 5;
  
  private int priority;
  public void setpriority (int priority) {this.priority = priority;
  public int getpriority () {return this.priority;
    public void Run () {Thread.CurrentThread (). SetPriority (priority);
    int threadId = id++;
      while (cutdown--> 0) {double d = 1.2;
      while (D < 10000) d = d + (MATH.E + Math.PI)/n;
    System.out.println ("#" + ThreadId + "(" + Cutdown + ")"); }} public class Basic {public static void main (String args[]) {//for (int i = 0; i < i++) {//Elem
E = new Elem ();
if (i = = 0)//e.setpriority (thread.max_priority);
else//e.setpriority (thread.min_priority);
Thread t = new thread (e);
T.start ();
    Executorservice exec = Executors.newcachedthreadpool (); for (int i = 0; i < i++) {Elem e = newElem ();
      if (i = = 0) e.setpriority (thread.max_priority);
      else e.setpriority (thread.min_priority);
    Exec.execute (e);
  } exec.shutdown (); }
}

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.