class Semaphore{
// 计数器
int count;
// 等待队列
Queue queue;
// 初始化操作
Semaphore(int c){
this.count=c;
}
//
void down(){
this.count--;
if(this.count<0){
// 将当前线程插入等待队列
// 阻塞当前线程
}
}
void up(){
this.count++;
if(this.count<=0) {
// 移除等待队列中的某个线程 T
// 唤醒线程 T
}
}
}
import java.io.*;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket();
// 超时时间
socket.setSoTimeout(3000);
// 连接本地,端口2000;超时时间3000ms
socket.connect(new InetSocketAddress(Inet4Address.getLocalHost(), 8000), 3000);
System.out.println("已发起服务器连接,并进入后续流程~");
System.out.println("客户端信息:" + socket.getLocalAddress() + " P:" + socket.getLocalPort());
System.out.println("服务器信息:" + socket.getInetAddress() + " P:" + socket.getPort());
try {
// 发送接收数据
todo(socket);
} catch (Exception e) {
System.out.println("异常关闭");
}
// 释放资源
socket.close();
System.out.println("客户端已退出~");
}
private static void todo(Socket client) throws IOException {
// 构建键盘输入流
InputStream in = System.in;
BufferedReader input = new BufferedReader(new InputStreamReader(in));
// 得到Socket输出流,并转换为打印流
OutputStream outputStream = client.getOutputStream();
PrintStream socketPrintStream = new PrintStream(outputStream);
// 得到Socket输入流,并转换为BufferedReader
InputStream inputStream = client.getInputStream();
BufferedReader socketBufferedReader = new BufferedReader(new InputStreamReader(inputStream));
boolean flag = true;
do {
// 键盘读取一行
String str = input.readLine();
// 发送到服务器
socketPrintStream.println(str);
// 从服务器读取一行
String echo = socketBufferedReader.readLine();
if ("bye".equalsIgnoreCase(echo)) {
flag = false;
}else {
System.out.println(echo);
}
}while (flag);
// 资源释放
socketPrintStream.close();
socketBufferedReader.close();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8000);
System.out.println("服务器准备就绪~");
System.out.println("服务器信息:" + server.getInetAddress() + " P:" + server.getLocalPort());
// 等待客户端连接
for (; ; ) {
// 得到客户端
Socket client = server.accept();
// 客户端构建异步线程
ClientHandler clientHandler = new ClientHandler(client);
// 启动线程
clientHandler.start();
}
}
/**
* 客户端消息处理
*/
private static class ClientHandler extends Thread {
private Socket socket;
private boolean flag = true;
ClientHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
super.run();
System.out.println("新客户端连接:" + socket.getInetAddress() +
" P:" + socket.getPort());
try {
// 得到打印流,用于数据输出;服务器回送数据使用
PrintStream socketOutput = new PrintStream(socket.getOutputStream());
// 得到输入流,用于接收数据
BufferedReader socketInput = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
do {
// 客户端拿到一条数据
String str = socketInput.readLine();
if ("bye".equalsIgnoreCase(str)) {
flag = false;
// 回送
socketOutput.println("bye");
} else {
// 打印到屏幕。并回送数据长度
System.out.println(str);
socketOutput.println("回送:" + str.length());
}
} while (flag);
socketInput.close();
socketOutput.close();
} catch (Exception e) {
System.out.println("连接异常断开");
} finally {
// 连接关闭
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("客户端已退出:" + socket.getInetAddress() +
" P:" + socket.getPort());
}
}
}package com.flink;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.mortbay.util.ajax.JSON;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class sendTest {
private static CloseableHttpClient httpClient;
// private static HttpPost httpPost;
public static void main(String[] args) throws Exception {
int Total= 1000000;
int Num = 0;
httpClient = HttpClients.createDefault();
long Starttime = System.currentTimeMillis();
for(int i = 0; i< Total; i++){
List<Object> List = new ArrayList<>();
Map<String, Object> map1 = new HashMap<>();
Map<String, String> tags = new HashMap<>();
tags.put("operationValue", "HDFF88FFF");
tags.put("gateMac", "00E2690CDFFD");
map1.put("metric", 10);
map1.put("value", i);
map1.put("tags", tags);
// System.out.println(i + "条数据");
map1.put("timestamp", Starttime/1000 + i + 3000000);
List.add(map1);
Num += 1;
if (Num >= 200) {
long Starttime1 = System.currentTimeMillis();
sendPost("http://192.168.3.101:4242/api/put?async", List);
long Endtime1 = System.currentTimeMillis();
System.out.println("==============================opentsdb写入耗时=============================: " + (Endtime1-Starttime1));
Num = 0;
List.clear();
}
}
long EndTime = System.currentTimeMillis();
System.out.println(Starttime);
System.out.println(EndTime);
System.out.println("处理" + Total + "条数据, 消耗: " + (EndTime - Starttime));
}
public static void sendPost(final String url, List<Object> list) throws IOException {
final HttpPost httpPost = new HttpPost(url);
if (! list.isEmpty()) {
// System.out.println(list);
String string = JSON.toString(list);
StringEntity entity = new StringEntity(string, "utf-8");
entity.setContentEncoding("utf-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
httpClient.execute(httpPost);
}
}
}#coding:utf-8
import time
import requests
import uuid
import math
import random
import time, threading
start = time.time()
start_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
def get_value(num):
return math.sin(num)+1
def send_json(json, s, ip):
url = "http://%s:4242/api/put" % ip
r = s.post(url, json=json)
return r.text
def main(metric, ip, Num):
s = requests.Session()
a = int(time.time() + 2000000) - Num
# print(a)
ls = []
k = random.randint(0, 10000)
value = "转速"
# value = "转速".encode('unicode_escape')
# value = value.replace("\\u", "")
for i in range(1, Num):
print("%s: %s条数据" % (ip, i))
time.sleep(2)
json = {
"metric": metric,
"timestamp": a,
"value": "sdfasd",
"tags": {
"operationValue": "HDFF88FFF",
"gateMac": "00E2690CDFFD",
}
}
a += 1
k += 0.01
ls.append(json)
if len(ls) == 1:
send_json(ls, s, ip)
ls = []
send_json(ls, s, ip)
print("%s: %s条数据" % (ip, Num))
print("开始时间: %s" % start_time)
print("时间: %s" % time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
import json
def read_opentsdb():
queries = [{
"metric": 53,
"tags": {"operationValue": "WeldCurrent"},
"aggregator": "none",
}]
url = "http://192.168.3.101:4242/api/query"
post_dict = {
"start": 1551285963,
"end": 1551285965,
"queries": queries
}
ret = requests.post(url, data=json.dumps(post_dict))
data_ret = ret.json()
print(data_ret)
def start_opentsdb():
main("12", "192.168.3.101", 1000000)
if __name__ == "__main__":
start_opentsdb()
# read_opentsdb()
package com.thinkInJava.test;
/**
* 作用:测试java中的三种移位运算符
* Tips:关于无符号右移(>>>)和java对于负数的右移操作
* 例中输出:
* b: 11111111111111111111111111010011
* b>>2: 11111111111111111111111111110100
* b>>>2: 00111111111111111111111111110100
* 可以看出java对于负数的右移是直接把左边的高位全部用1来填充,
* 若为无符号右移,则是直接把左边的高位全部用0来填充
*/
public class BinaryOperation {
public static void main(String[] args) {
int a = 55;
int b = -45;
//正数的二进制
System.out.println("a: " + Integer.toBinaryString(a));
//负数的二进制
System.out.println("b: " + Integer.toBinaryString(b));
//正数有符号右移两位的二进制
System.out.println("a >> 2: "+ Integer.toBinaryString(a >> 2));
//负数有符号右移两位的二进制
System.out.println("b >> 2: "+ Integer.toBinaryString(b >> 2));
//正数无符号右移两位的二进制
System.out.println("a >>> 2: "+ Integer.toBinaryString(a >>> 2));
//正数无符号右移两位的十进制依旧是个正数
System.out.println(a >>> 2);
//负数无符号右移两位的二进制
System.out.println("b >>> 2: "+ Integer.toBinaryString(b >>> 2));
//负数无符号右移两位的十进制变成了一个正数
System.out.println(b >>> 2);
}
}
a: 110111
b: 11111111111111111111111111010011
a >> 2: 1101
b >> 2: 11111111111111111111111111110100
a >>> 2: 1101
13
b >>> 2: 111111111111111111111111110100
1073741812