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{
@Override
public 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 block
e.printStackTrace();
}
}
}
}
class B extends Thread{
@Override
public 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 block
e.printStackTrace();
}
}
}
}
class C extends Thread{
@Override
public 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 block
e.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{
@Override
public 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 block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println("数据第"+i+"次复制完毕");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.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 {
@Override
public 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 block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println("图片第"+i+"次复制完毕");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.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{
@Override
public void run() {
for(int i=0;i<4;i++){
System.out.println("left线程:“口”");
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Right extends Thread{
@Override
public 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 block
e.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;
}
@Override
public void run() {
for(int i=0;i<10;i++){
container.getOne();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}