Synchronized list in java Collections Framework - list

In the below Java code, I am trying to create a synchronized List from normal ArrayList and then performing the multithreading operation but still it gives ConcurrentModificationException. Can someone tell me what is wrong here?
package Concurrent_Collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
*
* #author Nitesh Agrawal
*
*/
public class DemoForConcurrentModificationException extends Thread {
static List<Integer> myList = new ArrayList<Integer>();
static List<Integer> synchronizedList;
public static void main(String[] args) {
System.out.println("Main Thread");
synchronizedList = Collections.synchronizedList(myList);
for (int i = 0; i < 10; i++) {
synchronizedList.add(i);
}
DemoForConcurrentModificationException t = new DemoForConcurrentModificationException();
t.start();
for (Integer k : synchronizedList) {
System.out.println("Main");
System.out.println(k);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void run() {
System.out.println("Child Thread");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronizedList.add(100);
}
}

Related

java.lang.NullPointerException: Cannot invoke "org.apache.poi.ss.usermodel.Sheet.getLastRowNum()"

public class testUtil {
static Sheet sheet;
static Workbook book;
public static String TESTDATA_SHEET_PATH = "C:\Users\Admin\eclipse-workspace\ExcellReader\src\main\java\testData\LoginTest.xlsx";
public static Object[][] getTestData(String Sheetname) {
FileInputStream file = null;
try {
file = new FileInputStream(TESTDATA_SHEET_PATH);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
book = WorkbookFactory.create(file);
} catch (EncryptedDocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sheet = book.getSheet(Sheetname);
Object[][] data = new Object[sheet.getLastRowNum()][sheet.getRow(0).getLastCellNum()];
for (int i = 0; i < sheet.getLastRowNum(); i++) {
for (int k = 0; k < sheet.getRow(0).getLastCellNum(); k++) {
data[i][k] = sheet.getRow(i + 1).getCell(k).toString();
System.out.println(data[i][k]);
}
}
return data;
} }
List item

grpc server won't release memory, is there a memory leak?

I build a grpc server with c++ and find that its memory won't be released after several requests. memory increase first, and if I keep sending requests, the memory stay at a peak value. After I stop sending requests, memory won't be released or little memory released. what's wrong with my code, memory should be released soon or keep as catch buffer?
class BaseCallData {
public:
BaseCallData(XFRProcessor *processor)
: processor_(processor), status_(CallStatus::CREATE) {}
virtual ~BaseCallData() = default;
void Proceed() {
if (status_ == CallStatus::CREATE) {
status_ = CallStatus::PROCESS;
Request();
} else if (status_ == CallStatus::PROCESS) {
NewCallData();
//TODO
OnRequest();
Response();
status_ = CallStatus::FINISH;
} else if (status_ == CallStatus::FINISH) {
delete this;
} else {
LOGGER_ERROR(Log::GetLog(), "wrong grpc status");
}
}
template<class RpcReq, class RpcRes, class ReqData, class RspData>
void WorkFlow(RpcReq &grpc_request,
RpcRes &grpc_response,
ServerContext &ctx,
ReqData &request_data,
RspData &response_data) {
ErrorCode error_code = RequestReader::Instance()->ReadRequest(grpc_request, ctx, request_data);
ReqTimer req_timer(request_data.log_id_, request_data.request_type_);
if (error_code == OK) {
error_code = processor_->Proceed(&request_data, &response_data, &req_timer);
response_data.error_code = error_code;
grpc_response = ResponseAssigner::Instance()->AssignResponse(response_data);
if (OK == error_code) {
req_timer.SetStatus(true);
} else {
LOGGER_WARN(Log::GetLog(),
"[REQ:{}][LOG:{}] fail to run, err[{}]",
request_data.GetRequestType(),
request_data.GetLogId(),
error_code);
req_timer.SetStatus(false, std::to_string(error_code));
}
} else {
LOGGER_WARN(Log::GetLog(), "fail to read request, err[{}]", error_code);
req_timer.SetStatus(false, std::to_string(error_code));
grpc_response.set_error_code(error_code);
grpc_response.set_error_msg(GetErrorMsg(error_code));
}
}
XFRProcessor *GetProcessor() {
return processor_;
}
private:
virtual void NewCallData() = 0;
virtual void Request() = 0;
virtual void Response() = 0;
virtual void OnRequest() = 0;
enum class CallStatus { CREATE, PROCESS, FINISH };
CallStatus status_;
XFRProcessor *processor_;
};
class DetectCallData : public BaseCallData {
public:
DetectCallData(::xfr::XFRService::AsyncService *service, ServerCompletionQueue *cq, XFRProcessor *processor)
: BaseCallData(processor), p_service_(service), p_cq_(cq), responder_(&ctx_) {
Proceed();
}
void NewCallData() override {
new DetectCallData(p_service_, p_cq_, GetProcessor());
}
void Request() override {
p_service_->RequestDetect(&ctx_, &request_, &responder_, p_cq_, p_cq_, this);
}
void Response() override {
responder_.Finish(response_, Status::OK, this);
}
void OnRequest() override {
WorkFlow(request_, response_, ctx_, request_data_, response_data_);
}
private:
ServerContext ctx_;
::xfr::XFRService::AsyncService *p_service_;
ServerCompletionQueue *p_cq_;
::xfr::DetectRequest request_;
::xfr::DetectResponse response_;
DetectRequest request_data_;
DetectResponse response_data_;
ServerAsyncResponseWriter<::xfr::DetectResponse> responder_;
};
class CompareCallData : public BaseCallData {
...
};
class MatchCallData : public BaseCallData {
...
};
class XFRServer final {
public:
XFRServer(const XFRServer &) = delete;
XFRServer &operator=(const XFRServer &) = delete;
XFRServer() {
Init();
builder_.AddListeningPort(address_, InsecureServerCredentials());
builder_.RegisterService(&service_);
builder_.SetMaxReceiveMessageSize(max_receive_size_);
builder_.SetMaxSendMessageSize(max_send_size_);
for (int i = 0; i < thread_num_; ++i) {
auto p_cq = builder_.AddCompletionQueue();
v_cq_.push_back(std::move(p_cq));
}
}
void Init() {
auto grpc_config = hobot::vision::xfr::ServerConfig::GetConfig()->GetSubConfig("grpc");
address_ = grpc_config->GetSTDStringValue("server_address");
if (address_.empty()) {
LOGGER_ERROR(Log::GetLog(), "fail to get server address: {}", address_);
exit(0);
}
thread_num_ = grpc_config->GetIntValue("server_thread_count", 300);
max_receive_size_ = grpc_config->GetIntValue("max_receive_message_bytes", 20971520);
max_send_size_ = grpc_config->GetIntValue("max_send_message_bytes", 20971520);
}
~XFRServer() {
server_->Shutdown();
//document shows that cq should always shutdown after server
for (auto &cq : v_cq_) {
cq->Shutdown();
}
}
void HandleRpcs(ServerCompletionQueue *cq) {
auto detect_processor = std::make_shared<DetectProcessor>();
auto compare_processor = std::make_shared<CompareProcessor>();
auto match_processor = std::make_shared<MatchProcessor>();
new DetectCallData(&service_, cq, detect_processor.get());
new CompareCallData(&service_, cq, compare_processor.get());
new MatchCallData(&service_, cq, match_processor.get());
void *tag{nullptr};
bool ok{false};
while (true) {
if (!cq->Next(&tag, &ok)) {
LOGGER_WARN(Log::GetLog(), "Server stream closed, quiting");
break;
}
if (ok) {
static_cast<BaseCallData *>(tag)->Proceed();
}
}
}
void run() {
server_ = builder_.BuildAndStart();
for (auto &cq:v_cq_) {
v_threads_.emplace_back(
std::thread([this, &cq] {
HandleRpcs(cq.get());
})
);
}
LOGGER_INFO(Log::GetLog(), "grpc server start working...");
v_threads_.emplace_back(
std::thread([&zk_register] {
zk_register.KeepPublished();
})
);
for (auto &t: v_threads_) {
t.join();
}
}
private:
std::string address_;
int thread_num_;
std::vector<std::thread> v_threads_;
std::vector<std::unique_ptr<ServerCompletionQueue>> v_cq_;
::xfr::XFRService::AsyncService service_;
std::unique_ptr<Server> server_;
ServerBuilder builder_;
int max_receive_size_;
int max_send_size_;
};
here may be one of the answer.
I didn't handle the state machine correctly.
while (true) {
if (!cq->Next(&tag, &ok)) {
LOGGER_WARN(Log::GetLog(), "Server stream closed, quiting");
break;
}
if (ok) {
static_cast<BaseCallData *>(tag)->Proceed();
}
}
when ok!=true, object won't be deleted. so every time when I shutdown the client with 'ctl+c', memory leak.

Retrasmit sse using jax-rs jetty

I try to re - transmit sse from an osgi plugin . I have this code and at the moment i can only read the sse from event source and print it out in the console correctly. The sse write part is not working properly and buffering instead. Is there any way to fix it and read and write from the same function?
Thanks in advance!
#ApplicationPath("MySensor")
public class MySensor extends ResourceConfig {
private static String sensorA = "smotion";
// private static String sensorB = "sdist";
// private static String sensorC = "slight";
private int id = 0;
private String idn = "";
public MySensor() {
super(MySensor.class, SseFeature.class);
}
// creates new broadcaster
private static SseBroadcaster BROADCASTER = new SseBroadcaster();
#MethodDescription(value = "sse")
#GET
#Consumes(SseFeature.SERVER_SENT_EVENTS)
#Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput getServerSentEvents() {
id = id + 1;
idn = sensorA + " " + id;
BROADCASTER.broadcast(new OutboundEvent.Builder().data(String.class, idn).build());
// System.out.println(BROADCASTER.);
String LocalNetworkIP = "192.168.1.134";
EventOutput eventOutput = new EventOutput();
Client client = ClientBuilder.newBuilder().register(SseFeature.class).build();
WebTarget target = client.target("http://" + LocalNetworkIP + "/" + sensorA);
EventInput eventInput = target.request().get(EventInput.class);
while (!eventInput.isClosed()) {
InboundEvent inboundEvent = eventInput.read();
if (inboundEvent == null) {
break; // connection has been closed
}
try {
// handleevent
// inboundEvent.readData(String.class);
System.out.println(inboundEvent.readData(String.class));
OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
eventBuilder.name(inboundEvent.getName());
eventBuilder.data(inboundEvent.readData(String.class));
OutboundEvent event = eventBuilder.build();
eventOutput.write(event);
BROADCASTER.add(eventOutput);
} catch (IOException e) {
throw new RuntimeException("Error when writing the event.", e);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
try {
eventOutput.close();
} catch (IOException ioClose) {
throw new RuntimeException("Error when closing the event output.", ioClose);
}
return eventOutput;
}
I Finally find it out!
#MethodDescription(value = "Return the Server Sent Event")
#GET
#Consumes(SseFeature.SERVER_SENT_EVENTS)
#Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput getServerSentEvents() {
id = id + 1;
idn = sensorA + " " + id;
final EventOutput eventOutput = new EventOutput();
Client client = ClientBuilder.newBuilder().register(SseFeature.class).build();
WebTarget target = client.target("http://" + LocalNetworkIP + "/" + sensorA);
final EventInput eventInput = target.request().get(EventInput.class);
new Thread(new Runnable() {
#Override
public synchronized void run() {
try {
while (!eventInput.isClosed()) {
// Thread.sleep(500);
InboundEvent inboundEvent = eventInput.read();
if (inboundEvent == null) {
break; // connection has been closed
}
try {
// handleevent
// inboundEvent.readData(String.class);
System.out.println(inboundEvent.readData(String.class));
OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
eventBuilder.name(inboundEvent.getName());
eventBuilder.data(inboundEvent.readData(String.class));
OutboundEvent event = eventBuilder.build();
eventOutput.write(event);
} catch (IOException e) {
try { //extra
eventOutput.close(); //extra
eventInput.close(); //extra
} catch (IOException ioClose) { //extra
throw new RuntimeException("Error when closing the event output internal.", ioClose); //extra
} //extra
throw new RuntimeException("Error when writing or reading the event.", e);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (!eventOutput.isClosed()) { //extra
eventOutput.close(); //extra
} //extra
if (!eventInput.isClosed()) { //extra
eventInput.close();
} //extra
} catch (IOException ioClose) {
throw new RuntimeException("Error when closing the event output.", ioClose);
}
}
}
}).start();
return eventOutput;
}
}

Connection Pooling using Apache common DBCP And DBUtils and concurrency

I'm investigating concurrency when doing multiple queries in different threads.
I am using Apache DBCP and DBUtils not because I want to complicate my life but because they should guarantee that the queries are handled correctly and so the concurrency.
However even with the above cool tools I get a:
Error : org.h2.jdbc.JdbcSQLException: Das Objekt wurde bereits geschlossen
The object is already closed [90007-148]
Error : java.lang.NullPointerException
Which is the same kind of error I got also when using Database and Connection objects by hand.
It happens one time every 5-6 runs of the program, but this is just a toy program, in a real world application this kind of errors would pop up continuously.
Below my example code
DatatTransaction.java
import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;
public class DataTransaction
{
private final static String username = "";
private final static String password = "";
private final static String url = "db" + File.separator + "persondb;create=true";
public static Connection connection = null;
public static BasicDataSource dataSource;
public DataTransaction(boolean setCon)
{
try
{
setConnectionTest();
}
catch (Exception e)
{
System.out.println("Error in Connection:" + e.toString());
}
}
public final void setConnectionTest() throws SQLException
{
try
{
if (dataSource == null)
{
dataSource = new BasicDataSource();
String driver = "org.h2.Driver";
try
{
dataSource.setDriverClassName(driver);
dataSource.setUrl("jdbc:h2:"+url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setMaxActive(100);
dataSource.setMaxWait(10000);
dataSource.setMaxIdle(10);
if (connection == null || connection.isClosed())
{
connection = dataSource.getConnection();
}
}
catch (SQLException e)
{
System.out.println("Could not connect to the database msg :" + e.getMessage());
}
}
else
{
connection = dataSource.getConnection();
}
}
catch (Exception e)
{
System.out.println("open connection exception" + e);
}
}
}
and DBTest2.java
package dbtest;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class DBTest2
{
public static void main(String[] args)
{
try
{
new Thread(db1).start();
new Thread(db2).start();
}
catch (Exception e)
{
System.out.println("MM : Error : " + e);
}
}
private static Runnable db1 = new Runnable()
{
public void run()
{
try
{
for (int i = 0; i < 50; i++)
{
DBTest2 dBTest = new DBTest2();
List<Object[]> list1 = dBTest.DB1();
for (Object[] object : list1)
{
System.out.println("DB1 : FirstName : " + object[0] + " Lastname: " + object[1]);
}
}
}
catch (Exception e)
{
System.out.println("Error : " + e);
}
}
};
private static Runnable db2 = new Runnable()
{
public void run()
{
try
{
for (int i = 0; i < 50; i++)
{
DBTest2 dBTest = new DBTest2();
List<Object[]> list = dBTest.DB2();
for (Object[] object : list)
{
System.out.println("DB2 : FirstName : " + object[0] + " Lastname: " + object[1]);
}
}
}
catch (Exception e)
{
System.out.println("Error : " + e);
}
}
};
public List<Object[]> DB1()
{
try
{
DataTransaction dt = new DataTransaction(true);
Connection conn = dt.connection;
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select NAME,SURNAME from PERSON");
ResultSetMetaData rsmd = rs.getMetaData();
int dataCnt = rsmd.getColumnCount();
List<Object[]> list = new ArrayList<Object[]>();
while (rs.next())
{
Object[] data = new Object[dataCnt];
for (int i = 0; i < dataCnt; i++)
{
data[i] = rs.getString(i + 1);
}
list.add(data);
}
conn.close();
return list;
}
catch (Exception e)
{
System.out.println("Error : " + e);
return null;
}
}
public List<Object[]> DB2()
{
try
{
DataTransaction dt = new DataTransaction(true);
Connection conn = dt.connection;
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select NAME,SURNAME from PERSON");
ResultSetMetaData rsmd = rs.getMetaData();
int dataCnt = rsmd.getColumnCount();
List<Object[]> list = new ArrayList<Object[]>();
while (rs.next())
{
Object[] data = new Object[dataCnt];
for (int i = 0; i < dataCnt; i++)
{
data[i] = rs.getString(i + 1);
}
list.add(data);
}
conn.close();
return list;
}
catch (Exception e)
{
System.out.println("Error : " + e);
return null;
}
}
}
You should have a read of derby pitfalls in it there are some common pitfalls, the one you are interested in is:
"Executing a Statement automatically closes any existing open ResultSet generated by an earlier execution of that Statement. If threads share Statements, one thread could close another's ResultSet. In many cases, it is easier to assign each thread to a distinct Connection. "
Ok so the problem with your code is DatatTransaction.java, change the code by removing the static variable connection and add this method:
public final Connection getConnection()
{
Connection conn = null;
try
{
conn = dataSource.getConnection();
}
catch (SQLException e)
{
System.out.println("Could not connect to the database msg :" + e.getMessage());
}
return conn;
}
Now it will no longer give any problem (by the way, if you comment the System.out.println() in your example you will see more quickly and more often the concurrency errors which are otherwise mitigated by the time needed to print to the console).
As for the "exceptionorg.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object " that's because you are not closing the resources correctly by commenting the close() methods, so you quickly run out of connections in the pool.

How to implement a multicast socket in c++

I'm a c# programmer and i need to implement a multicast socket in c++.
I've try to google search it and didnt find much help.
So if someone could give me some links to a good c++ multicast socket tutorial it will be highly appreciated.
My c# socket implementation looks like this:
public class UdpMulticast
{
private Socket s;
private Thread listenThread;
private string mcastGroup;
private int port;
public UdpMulticast(string mcastGroup, int port)
{
this.mcastGroup = mcastGroup;
this.port = port;
}
private void Listen()
{
while (true)
{
try
{
byte[] b = new byte[1024];
Thread.Sleep(1);
int recv = s.Receive(b);
if (OnNewDataRecv != null)
{
byte[] tmp = new byte[recv];
for (int i = 0; i < recv; i++)
{
tmp[i] = b[i];
}
byte[] decByte = Encryption.Decrypt(tmp);
if(this.OnNewDataRecv !=null)
this.OnNewDataRecv(decByte, decByte.Length);
}
if (s == null)
{
break;
}
}
catch (ThreadAbortException)
{
break;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
public delegate void newData4Send(byte[] data, int dataLen);
public event newData4Send OnNewDataRecv;
public bool StartListen()
{
bool ret = false;
try
{
if (s == null)
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}
if (s != null)
{
Console.WriteLine("PORT multi cast :" + port);
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port);
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
s.Bind(ipep);
IPAddress ip = IPAddress.Parse(mcastGroup);
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, IPAddress.Any));
s.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastTimeToLive, int.Parse("1"));
listenThread = new Thread(new ThreadStart(Listen));
listenThread.IsBackground = true;
}
if (listenThread != null)
listenThread.Start();
ret = true;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return ret;
}
public void StopListen()
{
if (listenThread != null)
{
if (listenThread.IsAlive)
{
listenThread.Abort();
listenThread = null;
}
}
if (s != null)
{
s.Close();
s = null;
}
}
public void Send(byte[] data, int len)
{
if (s == null)
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}
if (s != null)
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(mcastGroup), port);
byte[] encByte = Encryption.Encrypt(data);
s.SendTo(encByte, encByte.Length, SocketFlags.None, ipep);
}
else Console.WriteLine("s is NULL");
}
}
I think i found something about it in wcf but i cant find a good tutorial.
There's no Socket class or sth. similar in plain C++. I suggest using a framework like Boost.Asio.
http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/examples.html
There is a multicast example in the documentation.