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             try {
     71                 sleep(1000);
     72             } catch (Exception ex) {
     73             }
     74         }
     75     }
     76 }
     77 
     78 class ThreadTestSub implements Runnable {
     79     public void run() {
     80         System.out.print("@ Thread running\n");
     81 
     82         try {
     83             Thread.currentThread().setDaemon(true);
     84             System.out.print("@ FAILED: setDaemon() succeeded\n");
     85         } catch (IllegalThreadStateException itse) {
     86             System.out.print("@ Got expected setDaemon exception\n");
     87         }
     88 
     89         //if (true)
     90         //    throw new NullPointerException();
     91         try {
     92             Thread.sleep(2000);
     93         }
     94         catch (InterruptedException ie) {
     95             System.out.print("@ Interrupted!\n");
     96         }
     97         finally {
     98             System.out.print("@ Thread bailing\n");
     99         }
    100     }
    101 }
    102