Java interview question: if an array [2, 4, 6, 2,] is known, sort the Array (in descending order, the tool class cannot be used for sorting) and create two threads to output the sorted array alternately, thread name customization,

Source: Internet
Author: User

Java interview question: if an array [2, 4, 6, 2,] is known, sort the Array (in descending order, the tool class cannot be used for sorting) and create two threads to output the sorted array alternately, thread name customization,

Package com. swift; import java. util. arrays; import java. util. comparator; public class ArrayThread_Test {public static void main (String [] args) {/** an array [,] is known and sorted (in descending order, the tool class cannot be used for sorting). Two threads are created to output the sorted array alternately. The thread name is customized */Integer [] arr = new Integer [] {2, 4, 6, 2, 1, 5}; // sort Arrays by array tool class. sort (arr, new Comparator <Integer> () {@ Override public int compare (Integer arg0, Integer arg1) {int num = arg 1-arg0; return num ;}}); // two-line quick sorting Integer [] arr2 = new Integer [] {7, 2, 4, 16, 2, 13, 5}; kuaisu (arr2, 0, arr2.length-1); for (Integer I: arr2) {System. out. print (I + "~ "+" \ R \ n ");} ArrayThread a = new ArrayThread (arr); Thread t1 = new Thread (a," Thread 1 "); thread t2 = new Thread (a, "Thread 2"); t1.start (); t2.start ();} private static void kuaisu (Integer [] arr2, int left, int right) {if (left> = right) // stop return when there is only one number after recursion; int key = arr2 [left]; int I = left; int j = right; while (I! = J) {while (arr2 [j]> key & I <j) {j --;} while (arr2 [I] <= key & I <j) {I ++;} if (I <j) {int temp; temp = arr2 [I]; arr2 [I] = arr2 [j]; arr2 [j] = temp;} arr2 [left] = arr2 [I]; arr2 [I] = key; kuaisu (arr2, left, j-1 ); kuaisu (arr2, I + 1, right );}}

The above is the test class, which performs fast sorting and tool class sorting.

Creates an object that implements the Runnable interface and transmits parameters.

Create two threads and start the thread

Package com. swift; public class ArrayThread implements Runnable {Integer [] arr; int index = 0; boolean flag = true; public ArrayThread (Integer [] arr) {this. arr = arr ;}@ Override public void run () {// for Loop is not suitable for multithreading. Although the content of an object is also executed multiple times, because the lock is in the for loop while (true) {synchronized (this) {if (index> = arr. length) {this. Y (); // Why can't other threads wake up? The virtual machine does not stop break;} System. out. println (Thread. currentThread (). getName () + "output" + arr [index]); index ++; if (flag) {flag = false; this. Y (); try {this. wait ();} catch (InterruptedException e) {e. printStackTrace () ;}} else {flag = true; this. Y (); try {this. wait ();} catch (InterruptedException e) {e. printStackTrace ();}}}}}}

Wake up other threads through notify, stop their own threads through wait, and use the flag to switch threads alternately.

Related Article

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.