| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fa0ad64 commit de89ea8
435 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,76 @@ | |||
| 1 | + /* $Id: $ | ||
| 2 | + */ | ||
| 3 | + package com.oreilly.demo.android.ch02.filllisttask; | ||
| 4 | + | ||
| 5 | + import java.util.ArrayList; | ||
| 6 | + import java.util.List; | ||
| 7 | + | ||
| 8 | + | ||
| 9 | + /** | ||
| 10 | + * Demonstrate cooperative concurrent programming | ||
| 11 | + */ | ||
| 12 | + public class FillListTask implements Runnable { | ||
| 13 | + private final int size; | ||
| 14 | + | ||
| 15 | + private List<String> strings; | ||
| 16 | + | ||
| 17 | + /** @param size the size of the desired list */ | ||
| 18 | + public FillListTask(int size) { | ||
| 19 | + this.size = size; | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + /** @return true if the list has been filled */ | ||
| 23 | + public synchronized boolean isFinished() { | ||
| 24 | + return null != strings; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + /** @return null or the filled list */ | ||
| 28 | + public synchronized List<String> getList() { | ||
| 29 | + return strings; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + /** @see java.lang.Runnable#run() */ | ||
| 33 | + @Override | ||
| 34 | + public void run() { | ||
| 35 | + List<String> strs = new ArrayList<String>(size); | ||
| 36 | + try { | ||
| 37 | + for (int i = 0; i < size; i++ ) { | ||
| 38 | + Thread.sleep(2000); | ||
| 39 | + strs.add("element " + String.valueOf(i)); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + synchronized (this) { | ||
| 43 | + strings = strs; | ||
| 44 | + this.notifyAll(); | ||
| 45 | + } | ||
| 46 | + } | ||
| 47 | + catch (InterruptedException e) { | ||
| 48 | + // catch interrupted exception outside loop, | ||
| 49 | + // since interrupted exception is a sign that | ||
| 50 | + // the thread should quit. | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + | ||
| 55 | + /** | ||
| 56 | + * Waits for the fill array task to complete | ||
| 57 | + * | ||
| 58 | + * @param args unused | ||
| 59 | + * @throws InterruptedException if slumber interrupted | ||
| 60 | + */ | ||
| 61 | + public static void main(String[] args) | ||
| 62 | + throws InterruptedException | ||
| 63 | + { | ||
| 64 | + FillListTask task = new FillListTask(7); | ||
| 65 | + | ||
| 66 | + new Thread(task).start(); | ||
| 67 | + | ||
| 68 | + synchronized (task) { | ||
| 69 | + while ( !task.isFinished()) { | ||
| 70 | + task.wait(); | ||
| 71 | + } | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + System.out.println("Array full: " + task.getList()); | ||
| 75 | + } | ||
| 76 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,6 @@ | |||
| 1 | + <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | + <classpath> | ||
| 3 | + <classpathentry kind="src" path="src"/> | ||
| 4 | + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
| 5 | + <classpathentry kind="output" path="eclipse/classes"/> | ||
| 6 | + </classpath> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,17 @@ | |||
| 1 | + <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | + <projectDescription> | ||
| 3 | + <name>FillListTask</name> | ||
| 4 | + <comment></comment> | ||
| 5 | + <projects> | ||
| 6 | + </projects> | ||
| 7 | + <buildSpec> | ||
| 8 | + <buildCommand> | ||
| 9 | + <name>org.eclipse.jdt.core.javabuilder</name> | ||
| 10 | + <arguments> | ||
| 11 | + </arguments> | ||
| 12 | + </buildCommand> | ||
| 13 | + </buildSpec> | ||
| 14 | + <natures> | ||
| 15 | + <nature>org.eclipse.jdt.core.javanature</nature> | ||
| 16 | + </natures> | ||
| 17 | + </projectDescription> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,53 @@ | |||
| 1 | + /* $Id: $ | ||
| 2 | + */ | ||
| 3 | + package com.oreilly.demo.android.ch02.sharedlisttask; | ||
| 4 | + | ||
| 5 | + import java.util.Vector; | ||
| 6 | + | ||
| 7 | + | ||
| 8 | + /** | ||
| 9 | + * Demonstrate that atomic methods may not be sufficient | ||
| 10 | + */ | ||
| 11 | + public class SharedListTask implements Runnable { | ||
| 12 | + private final Vector<String> list; | ||
| 13 | + | ||
| 14 | + /** @param l the list to share */ | ||
| 15 | + public SharedListTask(Vector<String> l) { | ||
| 16 | + this.list = l; | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + /** @see java.lang.Runnable#run() */ | ||
| 20 | + @Override | ||
| 21 | + public void run() { | ||
| 22 | + // the size of the list is obtained early | ||
| 23 | + int s = list.size(); | ||
| 24 | + | ||
| 25 | + while (true) { | ||
| 26 | + for (int i = 0; i < s; i++ ) { | ||
| 27 | + // throws IndexOutOfBoundsException!! | ||
| 28 | + // when the list is size 3, and s is 4. | ||
| 29 | + System.out.println(list.get(i)); | ||
| 30 | + } | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + /** | ||
| 35 | + * @param args ignored | ||
| 36 | + */ | ||
| 37 | + public static void main(String[] args) { | ||
| 38 | + Vector<String> list = new Vector<String>(); | ||
| 39 | + list.add("one"); | ||
| 40 | + list.add("two"); | ||
| 41 | + list.add("three"); | ||
| 42 | + list.add("four"); | ||
| 43 | + | ||
| 44 | + new Thread(new SharedListTask(list)).start(); | ||
| 45 | + | ||
| 46 | + try { Thread.sleep(2000); } | ||
| 47 | + catch (InterruptedException e) { /* ignore */} | ||
| 48 | + | ||
| 49 | + // the data structure is fully synchronized, | ||
| 50 | + // but that only protects the individual methods! | ||
| 51 | + list.remove("three"); | ||
| 52 | + } | ||
| 53 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,6 @@ | |||
| 1 | + <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | + <classpath> | ||
| 3 | + <classpathentry kind="src" path="src"/> | ||
| 4 | + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
| 5 | + <classpathentry kind="output" path="eclipse/classes"/> | ||
| 6 | + </classpath> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,17 @@ | |||
| 1 | + <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | + <projectDescription> | ||
| 3 | + <name>SharedListTask</name> | ||
| 4 | + <comment></comment> | ||
| 5 | + <projects> | ||
| 6 | + </projects> | ||
| 7 | + <buildSpec> | ||
| 8 | + <buildCommand> | ||
| 9 | + <name>org.eclipse.jdt.core.javabuilder</name> | ||
| 10 | + <arguments> | ||
| 11 | + </arguments> | ||
| 12 | + </buildCommand> | ||
| 13 | + </buildSpec> | ||
| 14 | + <natures> | ||
| 15 | + <nature>org.eclipse.jdt.core.javanature</nature> | ||
| 16 | + </natures> | ||
| 17 | + </projectDescription> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,37 @@ | |||
| 1 | + <?xml version="1.0" encoding="utf-8"?> | ||
| 2 | + <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| 3 | + package="com.oreilly.demo.pa.ch01.testapp" | ||
| 4 | + android:versionCode="1" | ||
| 5 | + android:versionName="1.0"> | ||
| 6 | + | ||
| 7 | + <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | ||
| 8 | + <uses-permission android:name="android.permission.CALL_PHONE" /> | ||
| 9 | + <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission> | ||
| 10 | + <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission> | ||
| 11 | + <uses-permission android:name="android.permission.INTERNET" /> | ||
| 12 | + | ||
| 13 | + | ||
| 14 | + <application android:icon="@drawable/icon" android:label="@string/app_name" | ||
| 15 | + android:debuggable="true"> | ||
| 16 | + | ||
| 17 | + <activity android:name=".TestActivity" | ||
| 18 | + android:label="Test Activity"> | ||
| 19 | + <intent-filter> | ||
| 20 | + <action android:name="android.intent.action.MAIN"/> | ||
| 21 | + <category android:name="android.intent.category.LAUNCHER"/> | ||
| 22 | + </intent-filter> | ||
| 23 | + </activity> | ||
| 24 | + | ||
| 25 | + <provider android:name=".TestProvider" | ||
| 26 | + android:authorities= "com.oreilly.demo.pa.ch11.video.FinchVideo" | ||
| 27 | + /> | ||
| 28 | + | ||
| 29 | + <service android:name=".TestService" | ||
| 30 | + android:label="Test Service"/> | ||
| 31 | + | ||
| 32 | + <receiver | ||
| 33 | + android:name=".TestBroadcastReceiver" | ||
| 34 | + android:label="Test Broadcast Receiver"/> | ||
| 35 | + </application> | ||
| 36 | + <uses-sdk android:minSdkVersion="7" /> | ||
| 37 | + </manifest> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,10 @@ | |||
| 1 | + <?xml version="1.0" encoding="utf-8"?> | ||
| 2 | + | ||
| 3 | + <LinearLayout | ||
| 4 | + xmlns:android="http://schemas.android.com/apk/res/android" | ||
| 5 | + android:layout_width="fill_parent" | ||
| 6 | + android:layout_height="fill_parent" | ||
| 7 | + android:id="@+id/video_layout" | ||
| 8 | + android:orientation="vertical" | ||
| 9 | + > | ||
| 10 | + </LinearLayout> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + <resources> | ||
| 2 | + <string name="app_name">Test Application</string> | ||
| 3 | + </resources> | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments