1、设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。考虑到线程的安全性写出程序。
package classlib.Day0311.Practice;
/**
* @author 35125
*/
public class Test01 {
private int j;
public static void main(String[] args) {
Test01 t = new Test01();
Add add = t.new Add();
Sub sub = t.new Sub();
for (int i = 0; i < 2; i++) {
Thread tt = new Thread(add);
tt.start();
tt = new Thread(sub);
tt.start();
}
}
private synchronized void add(){
j++;
System.out.println(Thread.currentThread().getName()+"--->增加"+j);
}
private synchronized void sub(){
j--;
System.out.println(Thread.currentThread().getName()+"--->减去"+j);
}
class Add implements Runnable{
@Override
public void run() {
for (int i = 0; i < 20; i++) {
add();
}
}
}
class Sub implements Runnable{
@Override
public void run() {
for (int i = 0; i < 20; i++) {
sub();
}
}
}
}
2、同时下载四张图片。
package classlib.Day0311.Practice;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
/**
* @author 35125
*/
public class Test02 implements Runnable{
//图片地址
private String path;
//保存的文件名
private String name;
public Test02(String path, String name) {
this.path = path;
this.name = name;
}
@Override
public void run() {
Download dl = new Download();
dl.downloader(path,name);
System.out.println("下载了文件名为:"+name);
}
public static void main(String[] args) {
Test02 t1 = new Test02("https://image.baidu.com/search/detail?tn=baiduimagedetail&word=%E5%9F%8E%E5%B8%82%E5%BB%BA%E7%AD%91%E6%91%84%E5%BD%B1%E4%B8%93%E9%A2%98&album_tab=%E5%BB%BA%E7%AD%91&album_id=7&ie=utf-8&fr=albumsdetail&cs=1595072465,3644073269&pi=3977&pn=0&ic=0&objurl=https%3A%2F%2Ft7.baidu.com%2Fit%2Fu%3D1595072465%2C3644073269%26fm%3D193%26f%3DGIF","11.jpg");
Test02 t2 = new Test02("https://image.baidu.com/search/detail?tn=baiduimagedetail&word=%E5%9F%8E%E5%B8%82%E5%BB%BA%E7%AD%91%E6%91%84%E5%BD%B1%E4%B8%93%E9%A2%98&album_tab=%E5%BB%BA%E7%AD%91&album_id=7&ie=utf-8&fr=albumsdetail&cs=1956604245,3662848045&pi=3979&pn=2&ic=0&objurl=https%3A%2F%2Ft7.baidu.com%2Fit%2Fu%3D1956604245%2C3662848045%26fm%3D193%26f%3DGIF","22.jpg");
Test02 t3 = new Test02("https://img2.baidu.com/it/u=1835843610,1575206394&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=999","333.jpg");
Test02 t4 = new Test02("https://img1.baidu.com/it/u=2045922693,212563729&fm=253&fmt=auto&app=120&f=JPEG?w=1024&h=576","444.jpg");
new Thread(t1).start();
new Thread(t2).start();
new Thread(t3).start();
new Thread(t4).start();
}
}
//下载器
class Download{
//下载方法
public void downloader(String path,String name){
try {
FileUtils.copyURLToFile(new URL(path),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("io异常,下载方法有问题。");
}
}
}
3、通信软件的功能(基于UDP)
有客户端和服务器端,实现客户端和服务器相互不停的发送和接收消息。
要求发送和接收消息可以同时进行。
package classlib.Day0311.Practice;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class TalkSend implements Runnable{
DatagramSocket socket =null;
BufferedReader br =null;
private int fromPort;
//对方的ip和端口号
private String toIP;
private int toPort;
public TalkSend( int fromPort, String toIP, int toPort) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
DatagramSocket socket = new DatagramSocket(fromPort);
br = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try{
String data = br.readLine();
byte[] bytes = data.getBytes("UTF-8");
DatagramPacket dp = new DatagramPacket(bytes,0,data.length(),new InetSocketAddress(this.toIP,this.toPort));
socket.send(dp);
if (data.equals("bye")){
break;
}
}catch (Exception e){
e.printStackTrace();
}
}
socket.close();
}
}
package classlib.Day0311.Practice;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceive implements Runnable{
DatagramSocket socket = null;
private int port;
private String msgFrom;
public TalkReceive(int port,String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
try {
socket = new DatagramSocket(6666);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
byte[] container = new byte[1024];
DatagramPacket p = new DatagramPacket(container,0,container.length);
socket.receive(p);
//断开连接 bye
byte[] data = p.getData();
String s = new String(data, 0, data.length,"UTF-8");
System.out.println(msgFrom+":"+s);
if (s.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
package classlib.Day0311.Practice;
import javax.sound.midi.Receiver;
/**
* @author 35125
*/
public class TalkStu {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
package classlib.Day0311.Practice;
/**
* @author 35125
*/
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}