Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 import java.util.ArrayList;
     18 
     19 /**
     20  * Test some basic thread stuff.
     21  */
     22 public class Main {
     23     public static void main(String[] args) throws Exception {
     24         System.out.println("Initializing System.out...");
     25 
     26         MyThread[] threads = new MyThread[512];
     27         for (int i = 0; i < 512; i++) {
     28             threads[i] = new MyThread();
     29         }
     30 
     31         for (MyThread thread : threads) {
     32             thread.start();
     33         }
     34         for (MyThread thread : threads) {
     35             thread.join();
     36         }
     37 
     38         System.out.println("Thread count: " + MyThread.mCount);
     39 
     40         go();
     41         System.out.println("thread test done");
     42     }
     43 
     44     public static void go() {
     45         Thread t = new Thread(null, new ThreadTestSub(), "Thready", 7168);
     46 
     47         t.setDaemon(false);
     48 
     49         System.out.print("Starting thread '" + t.getName() + "'\n");
     50         t.start();
     51 
     52         try {
     53             t.join();
     54         } catch (InterruptedException ex) {
     55             ex.printStackTrace();
     56         }
     57 
     58         System.out.print("Thread starter returning\n");
     59     }
     60 
     61     /*
     62      * Simple thread capacity test.
     63      */
     64     static class MyThread extends Thread {
     65         static int mCount = 0;
     66         public void run() {
     67             synchronized (MyThread.class) {
     68                 ++mCount;
     69             }
     70         }
     71     }
     72 }
     73 
     74 class ThreadTestSub implements Runnable {
     75     public void run() {
     76         System.out.print("@ Thread running\n");
     77 
     78         try {
     79             Thread.currentThread().setDaemon(true);
     80             System.out.print("@ FAILED: setDaemon() succeeded\n");
     81         } catch (IllegalThreadStateException itse) {
     82             System.out.print("@ Got expected setDaemon exception\n");
     83         }
     84 
     85         //if (true)
     86         //    throw new NullPointerException();
     87         try {
     88             Thread.sleep(2000);
     89         }
     90         catch (InterruptedException ie) {
     91             System.out.print("@ Interrupted!\n");
     92         }
     93         finally {
     94             System.out.print("@ Thread bailing\n");
     95         }
     96     }
     97 }
     98