Java之进程与线程练习
生活随笔
收集整理的這篇文章主要介紹了
Java之进程与线程练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.設計一個線程類:創建3個子線程,每個線程分別打印數字,分別睡眠100,200,300ms —>每個執行都是20次
代碼:
package Homework; //1.設計一個線程類:創建3個子線程,每個線程分別打印數字,分別睡100,200,300 --->每個執行都是20次 public class Test1 {public static void main(String[] args) {A a=new A();B b=new B();C c=new C();a.start();b.start();c.start();} }class A extends Thread{@Overridepublic void run() {for(int i=0;i<20;i++){System.out.println("a線程"+i);try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} } class B extends Thread{@Overridepublic void run() {for(int i=0;i<20;i++){System.out.println("b線程"+i);try {Thread.sleep(200);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} } class C extends Thread{@Overridepublic void run() {for(int i=0;i<20;i++){System.out.println("c線程"+i);try {Thread.sleep(300);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} }部分運行結果:
2.創建兩個線程模擬下載數據,第一個線程負責下載10張圖片,第二個線程負責下載20條數據,分別休眠20毫秒和10毫秒。觀察打印情況。
代碼:
下載數據線程:
package Homework;import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;public class DownLoadData extends Thread{@Overridepublic void run() {for(int i=0;i<20;i++){FileInputStream fis=null;FileOutputStream fos=null;try {fis=new FileInputStream(new File("Photo\\a.txt"));fos=new FileOutputStream(new File("Example\\a"+i+".txt"));byte[] bs=new byte[2];int count=0;while((count=fis.read(bs))!=-1){fos.write(bs, 0, count);fos.flush();}} catch (FileNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {if(fis!=null){try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(fos!=null){try {fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}System.out.println("數據第"+i+"次復制完畢");try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} }下載圖片線程:
package Homework;import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;public class DownLoadPhoto extends Thread {@Overridepublic void run() {for(int i=0;i<10;i++){FileInputStream fis=null;FileOutputStream fos=null;try {fis=new FileInputStream(new File("Photo\\img1.png"));fos=new FileOutputStream(new File("Example\\img"+i+".png"));byte[] bs=new byte[2];int count=0;while((count=fis.read(bs))!=-1){fos.write(bs, 0, count);fos.flush();}} catch (FileNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {if(fis!=null){try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(fos!=null){try {fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}System.out.println("圖片第"+i+"次復制完畢");try {Thread.sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} }測試類:
package Homework;import java.io.File;public class Test2 {public static void main(String[] args) {//在當前工程下創建文件夾File file2=new File("Example");if(!file2.exists()){file2.mkdirs();}//創建線程對象,并啟動線程DownLoadData data=new DownLoadData();DownLoadPhoto photo=new DownLoadPhoto();data.start();photo.start();} }部分運行結果:
3.每個線程打印4個:左手是一個線程負責畫”□”,右手也是一個線程負責畫”O”,左每畫一個睡300,右每畫一個睡200.
代碼:
package Homework;public class Test3 {public static void main(String[] args) {Left left=new Left();Right right=new Right();left.start();right.start();} }class Left extends Thread{@Overridepublic void run() {for(int i=0;i<4;i++){System.out.println("left線程:“口”");try {Thread.sleep(300);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} } class Right extends Thread{@Overridepublic void run() {for(int i=0;i<4;i++){System.out.println("right線程:“o”");try {Thread.sleep(200);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} }運行結果
4.創建一個Container類,該類中有屬性 int count=200 ,
定義一個方法 getOne(){執行count的–}表示:每調用一次getOne()方法count數量減1。
三個線程操作Container類,每個線程執行10次,每執行一次sleep(100).保證數據的安全性
代碼:
package Homework; /*** 4.創建一個Container類,該類中有屬性 int count=200 ,* 定義一個方法 getOne(){執行count的--}表示每調用一次getOne()方法count數量減1。* 三個線程操作Container類,每個線程執行10次,每執行一次sleep(100).保證數據的安全性* @author Administrator**/ public class Test4 {public static void main(String[] args) {Container container=new Container();MyThread myThread=new MyThread(container);MyThread myThread2=new MyThread(container);MyThread myThread3=new MyThread(container);myThread.start();myThread2.start();myThread3.start();} }class Container{int count=200;public void getOne(){count--;System.out.println("當前剩余次數:"+count);} }class MyThread extends Thread{Container container;public MyThread(Container container) {this.container=container;}@Overridepublic void run() {for(int i=0;i<10;i++){container.getOne();try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} }總結
以上是生活随笔為你收集整理的Java之进程与线程练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java线程间通信
- 下一篇: Java之线程同步练习